【问题标题】:htaccess rewrite for codeigniter site为codeigniter网站重写htaccess
【发布时间】:2011-12-03 11:17:45
【问题描述】:

我想重写codeigniter url rewrite的代码。

我使用了以下代码。

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

RewriteRule ^admin$ administration
RewriteRule ^video/([0-9]+)/(.*)$ videos/details/$1/$1

它在删除 index.php 时效果很好。 但它不适用于url重写

我希望 example.com/admin 变成 example.com/administration

example.com/video/123/title 变为 example.com/videos/details/123/title

提前致谢, w3父亲

【问题讨论】:

  • 我很恼火,因为你不能在控制器的索引中使用额外的信息,你必须使用不同的方法 -> url 中不需要的东西。所以我很感兴趣什么对你有用。

标签: .htaccess codeigniter url-rewriting codeigniter-url


【解决方案1】:

我相信您需要做的就是将您的两条新规则放在现有规则之前。

RewriteEngine On
RewriteBase /whype

RewriteRule ^admin$ administration
RewriteRule ^video/([0-9]+)/(.*)$ videos/details/$1/$2

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]

【讨论】:

    【解决方案2】:

    您应该使用以下 .htaccess:

    <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteBase /
    
        #Removes access to the system folder by users.
        #Additionally this will allow you to create a System.php controller,
        #previously this would not have been possible.
        #'system' can be replaced if you have renamed your system folder.
        RewriteCond %{REQUEST_URI} ^system.*
        RewriteRule ^(.*)$ /index.php?/$1 [L]
    
        #When your application folder isn't in the system folder
        #This snippet prevents user access to the application folder
        #Submitted by: Fabdrol
        #Rename 'application' to your applications folder name.
        RewriteCond %{REQUEST_URI} ^application.*
        RewriteRule ^(.*)$ /index.php?/$1 [L]
    
        #Checks to see if the user is attempting to access a valid file,
        #such as an image or css document, if this isn't true it sends the
        #request to index.php
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^(.*)$ index.php?/$1 [L]
    </IfModule>
    
    <IfModule !mod_rewrite.c>
        # If we don't have mod_rewrite installed, all 404's
        # can be sent to index.php, and everything works as normal.
        # Submitted by: ElliotHaughin
    
        ErrorDocument 404 /index.php
    </IfModule> 
    

    然后将您的路由添加到 application/config 文件夹中的 routes.php。请参阅URI Routing 了解更多信息。

    【讨论】:

      猜你喜欢
      • 2016-03-17
      • 2012-10-31
      • 2012-09-10
      • 2016-10-13
      • 2014-01-07
      • 2012-12-09
      • 2015-02-03
      • 2012-11-28
      • 1970-01-01
      相关资源
      最近更新 更多