`
guowee
  • 浏览: 177252 次
  • 性别: 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/

分享到:
评论

相关推荐

    URLRewriting

    URL重写(URLRewriting)是一种在Web开发中常见的技术,主要用于优化网站的URL结构,使其更加用户友好和搜索引擎友好。这项技术的核心是通过修改HTTP请求的URL,使其看起来像是在访问静态页面,但实际上可能是在处理...

    在ASP.NET中实现Url Rewriting (一)

    ASP.NET中的URL重写是一种强大的技术,用于改进网站的可访问性和SEO(搜索引擎优化)。它涉及到将不友好的、复杂的URL转换为更简洁、更具描述性的格式,从而提高用户体验和搜索引擎的理解。在ASP.NET框架中,有多种...

    URLRewriting URL重写的最好控件

    URLRewriting是一个强大的工具,主要用于优化网站的URL结构,使其更友好、更易于理解和记忆,同时也提升了搜索引擎优化(SEO)的效果。在这个主题中,我们将深入探讨URL重写的基本概念、工作原理以及如何在实际应用...

    UrlRewriting组件

    UrlRewriting组件是一种在Web应用程序中广泛使用的工具,主要用于优化和管理URL,使其更符合用户友好和搜索引擎优化的标准。这个组件通常与ASP.NET框架结合使用,可以极大地改善网站的架构和功能。 首先,我们来...

    URL Rewriting in ASP.NET1.1

    URL重写是ASP.NET开发中的一个重要技术,尤其在ASP.NET 1.1版本中,它为网站提供了优化和增强功能的机会。URL重写允许开发者隐藏实际的页面路径,创建更友好的、搜索引擎优化(SEO)的URL,以及实现更加灵活的路由...

    Programmatically Setting Control Adapters for URL Rewriting and AJAX

    标题中的“Programmatically Setting Control Adapters for URL Rewriting and AJAX”指的是在编程环境中设置用于URL重写和AJAX的控件适配器的技术。URL重写是改变URL结构以优化SEO(搜索引擎优化)和提高用户体验的...

    URLRewriting 重定向

    URL重写(URLRewriting)是ASP.NET框架中一种重要的技术,它主要用于优化网站的URL结构,提升用户体验,以及在SEO(搜索引擎优化)方面发挥关键作用。URL重写技术允许我们将复杂的服务器端处理隐藏在简洁、友好的URL...

    在ASP.NET中实现Url Rewriting (四)

    在ASP.NET中实现Url Rewriting是一项重要的技术,它允许开发者将复杂的、动态生成的URL转换为简洁、易于理解和搜索引擎友好的格式。这篇文章主要探讨了如何在ASP.NET环境中使用HttpModule和HttpHandler来实现这一...

    在ASP.NET中实现Url Rewriting (三)

    在ASP.NET中实现Url Rewriting是一项重要的技术,它允许开发者将复杂的、动态生成的URL转换为简洁、易于理解和搜索引擎友好的格式。这篇文章主要探讨了如何在ASP.NET环境中使用HttpModule和HttpHandler来实现这一...

    URL Rewriting in ASP.NET

    在ASP.NET中,URL重写是一项重要的技术,它允许开发者隐藏实际的服务器路径,提供更加用户友好和SEO优化的URL。这项技术的核心在于通过修改HTTP请求的URL,使其符合我们期望的格式,而不改变实际处理请求的逻辑。...

    在ASP.NET中实现Url Rewriting (五)

    ### 在ASP.NET中实现Url Rewriting(五) #### 概述 本文主要聚焦于ASP.NET框架内实现URL重写技术的细节与实践方法。URL重写是一种常见的网站优化技术,它能够帮助改善用户体验、增强网站的安全性以及提高搜索引擎...

    UrlRewriting

    尝试依葫芦画瓢写了自己的一个UrlRewriting。本人资源分比较低,就希望各位支持了。 一、参数 1、Enabled:bool值,设置是否应用Url重写功能; 2、IsUseCache:bool,设置是否启用Cache功能;(写该功能...

    在ASP.NET中实现Url Rewriting (六)

    ASP.NET中的URL重写是一种强大的技术,用于优化和管理网站的动态URL,使其更符合用户的友好体验和搜索引擎优化(SEO)需求。在本篇中,我们将深入探讨如何使用ASP.NET来实现URL重写,以及这一过程涉及到的关键概念和...

    在ASP.NET中实现Url Rewriting (二)

    ASP.NET中的URL重写是一种强大的技术,用于改变网站的URL结构,从而提高SEO(搜索引擎优化)、用户体验和可维护性。本文主要关注如何在ASP.NET环境中实现URL重写,特别是通过ISAPI过滤器和HttpModules。 首先,理解...

    URL Rewriting

    **URL重写技术详解** URL重写是一种网页优化技术,它允许我们更改实际请求的URL,而不会影响页面内容的显示。在ASP.NET环境中,URL重写是提升用户体验、优化SEO(搜索引擎优化)以及实现更加友好的URL结构的重要...

    DotBBS 开源论坛 由ASP.NET+Access架构而成,当然也可以和MSSQL搭配,程序中使用有第三方的组件,像CuteEditor以及URL重写组件UrlRewriting.dll等。这个论坛界面挺清新,如上图示,功能可以满足中小型网站使用,Ajax操作,舒适度提高不少。

    DotBBS 开源论坛 由ASP.NET+Access架构而成,当然也可以和MSSQL搭配,程序中使用有第三方的组件,像CuteEditor以及URL重写组件UrlRewriting.dll等。这个论坛界面挺清新,如上图示,功能可以满足中小型网站使用,Ajax...

    ASP.NET中URL Routing和IIS上URL Rewriting的区别

    ***中URL Routing和IIS上URL Rewriting的区别主要体现在以下几个方面: 首先,URL Routing和URL Rewriting虽然都用于生成更加友好和搜索引擎优化的URL,但它们的实现原理和适用场景存在差异。URL Routing是*** MVC...

    UrlRewriting Filter-开源

    UrlRewriting Filter 是一个 servlet 过滤器,它自动将 request.encodeURL() 应用于 servlet 响应正文中的 url。 这允许用户不必担心手动将 request.encodeURL() 应用于这些 url 以进行会话传播。

Global site tag (gtag.js) - Google Analytics