URL重写以将对http://example.com/controller的所有请求重定向到http://example.com/index.php/controller而不更改网址

时间:2010-08-01 21:15:34

标签: apache mod-rewrite codeigniter url-rewriting

我想将http://example.com/controller的所有请求重定向到http://example.com/index.php/controller而不更改网址,这就是我的.htaccess文件的样子:

# Customized error messages.
ErrorDocument 404 /index.php

# Set the default handler.
DirectoryIndex index.php

# Various rewrite rules.
<IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^(.*)$ index.php/$1 [L,QSA]

  RewriteBase /
  RewriteCond %{HTTP_HOST} ^www.example.com [NC]
  RewriteRule ^(.*)$ http://example.com/$1 [R=301] 
</IfModule>

不幸的是,这不起作用。对http://example.com/controller的所有请求都会路由到主控制器,并且网址不会更改。 并且http://www.example.com/controller的所有请求都会被路由到http://example.com/index.php/controller,并且地址栏中的网址会发生变化。

1 个答案:

答案 0 :(得分:1)

将那些导致外部重定向的规则放在导致内部重定向的规则之前。所以:

RewriteEngine on
RewriteBase /

RewriteCond %{HTTP_HOST} =www.example.com [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]

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