URL Rewriting for CodeIgniter

本文介绍如何在CodeIgniter中去除URL中的'index.php'部分,通过使用.htaccess文件实现更简洁的URL结构。文章提供了两种解决方案,分别适用于本地开发环境和在线主机,并解释了实现原理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值