`
guowee
  • 浏览: 173918 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

URL Rewriting for CodeIgniter

阅读更多

What is URL Rewriting?

URL rewriting provides shorter and more relevant-looking links to web pages on your site. This improves the readability and the search rankings of your URLs. For example, URL “a” can be rewritten as URL “b”.

a) http://example.com/index.php?section=casting
b) http://example.com/casting

There are many articles on the web discussing the benefits of shorter and more relevant URLs. Let me add to them that you get to hide the used technology from both search engines and users. You’ll also have a better time migrating to a different platform in case you need to. For example, if you build your web application on top of ASP.NET and use URL Rewriting, you can easily migrate to PHP (Recommended, of course) without changing your links and hence without losing all the search-ranking score for those links.

CodeIgniter Default URLs

By default, CodeIgniter uses a segment-based approach to represent URLs. Unfortunately, CodeIgniter includes the annoying “index.php” file name in the URL. For example:

http://example.com/index.php/products/view/shoes.

Now, the CodeIgniter manual mentions that it’s very simple to remove the “index.php” part from the URL using the following .htaccess file:

 
1 RewriteEngine on
2 RewriteCond $1 !^(index\.php|images|robots\.txt)
3 RewriteRule ^(.*)$ /index.php/$1 [L]

Problem solved? Not really. This didn’t work on my local machine nor on my online hosting account (Dreamhost ). Why? I don’t know. I don’t care. I don’t currently have the time to find out why.

Having already installed Wordpress, I remembered that their .htaccess file works offline and online (at least in my case). I started playing around with it, checked some online resources, and devised two solutions. The local solution works on my local machine with the XAMPP server installed on it and the online solution works on my Dreamhost account.

The Local Solution

1 RewriteEngine On
2 RewriteBase /ci/
3 RewriteCond %{REQUEST_FILENAME} !-f
4 RewriteCond %{REQUEST_FILENAME} !-d
5 RewriteRule ^(.*)$ /ci/index.php/$1 [L]

The only change you need to make is to “ci” (on lines 2 and 5) which is the folder where you have your CodeIgniter application installed. In brief, this rewrite file tells your web server to apply the rewrite rule whenever a file or a directory is not found on the server. For example, if you invoke URL “c”, the “contact” folder is not found on your server (Since CodeIgniter files are in the “system” folder), and accordingly the URL is rewritten to “d”. This rewrite allows CodeIgniter to execute successfully (By using URL “d”) while giving you the benefits of shorter URLs (URL “c”).

c) http://localhost/ci/contact
d) http://localhost/ci/index.php/contact

The Online Solution


1 RewriteEngine On
2 RewriteBase /
3 RewriteCond %{REQUEST_FILENAME} !-f
4 RewriteCond %{REQUEST_FILENAME} !-d
5 RewriteRule ^(.*)$ /index.php?/$1 [L]

There are no changes that you need to make for this .htaccess file. However, there’s one important note to mention. The question mark after “index.php” on line 5 is needed on my online hosting account at Dreamhost. You might want to remove it if it doesn’t work on yours. Again, I didn’t have the time to investigate why this is the case. Please check the additional resources for more details.

Conclusion

I hope this post saves you a few headaches I had to go through to solve this problem. I would love to see such a solution coming out of the box with the next version of CodeIgniter. If you have any hints or additional information regarding URL rewriting in the context of CodeIgniter, please share them in the comments.

Additional Resources

CodeIgniter URLs
http://codeigniter.com/user_guide/general/urls.html

URL Rewriting for Beginners
http://www.addedbytes.com/apache/url-rewriting-for-beginners/

An easy way to test your RewriteRules against different URLs
http://civilolydnad.se/projects/rewriterule/

Dreamhost and CodeIgniter URLs
http://codeigniter.com/forums/viewthread/55620/

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics