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:
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
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
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/
分享到:
相关推荐
URL Rewriting in ASP.NET1.1 C#
URLRewriting 页面静态化.实现表态化用的,下载后直安装后直接用就OK了.
Programmatically Setting Control Adapters for URL Rewriting and AJAX。
分析如何使用微软提供的ASP.NET来对动态产生的URL地址进行网址重写。 网址重写是实现一种截取网址请求并将其进行处理后重新指向到一个指定的网址的过程。作者本人在对各种实现网址重写的技术进行研究和探讨后得出的...
URLRewriting 重定向 URLRewriting 重定向
URL Rewriting in ASP.NET
URLRewriting 重写 使用说明。如果你不想让别人看到你动态网页的直接路径时,就用它。
尝试依葫芦画瓢写了自己的一个UrlRewriting。本人资源分比较低,就希望各位支持了。 一、参数 1、Enabled:bool值,设置是否应用Url重写功能; 2、IsUseCache:bool,设置是否启用Cache功能;(写该功能...
看 看 吧 ,很 不 错 的 东 东 !
分析如何使用微软提供的ASP.NET来对动态产生的URL地址进行网址重写。 网址重写是实现一种截取网址请求并将其进行处理后重新指向到一个指定的网址的过程。作者本人在对各种实现网址重写的技术进行研究和探讨后得出的...
分析如何使用微软提供的ASP.NET来对动态产生的URL地址进行网址重写。 网址重写是实现一种截取网址请求并将其进行处理后重新指向到一个指定的网址的过程。作者本人在对各种实现网址重写的技术进行研究和探讨后得出的...
分析如何使用微软提供的ASP.NET来对动态产生的URL地址进行网址重写。 网址重写是实现一种截取网址请求并将其进行处理后重新指向到一个指定的网址的过程。作者本人在对各种实现网址重写的技术进行研究和探讨后得出的...
分析如何使用微软提供的ASP.NET来对动态产生的URL地址进行网址重写。 网址重写是实现一种截取网址请求并将其进行处理后重新指向到一个指定的网址的过程。作者本人在对各种实现网址重写的技术进行研究和探讨后得出的...
分析如何使用微软提供的ASP.NET来对动态产生的URL地址进行网址重写。 网址重写是实现一种截取网址请求并将其进行处理后重新指向到一个指定的网址的过程。作者本人在对各种实现网址重写的技术进行研究和探讨后得出的...
DotBBS 开源论坛 由ASP.NET+Access架构而成,当然也可以和MSSQL搭配,程序中使用有第三方的组件,像CuteEditor以及URL重写组件UrlRewriting.dll等。这个论坛界面挺清新,如上图示,功能可以满足中小型网站使用,Ajax...
在 ASP.NET 中执行 URL 重写
前面有2篇帖子提到了关于URL Routing的特性,但是发现有很多人误会URL Routing就是URl Rewriting,其实2个虽然都提供相似的功能(提高友好的URL方便搜索引起收录),但是2者的原理和运行周期是完全不一样的,本篇...
UrlRewriting Filter 是一个 servlet 过滤器,它自动将 request.encodeURL() 应用于 servlet 响应正文中的 url。 这允许用户不必担心手动将 request.encodeURL() 应用于这些 url 以进行会话传播。