使用.htaccess重写问题(从CodeIgniter URL中删除/index.php)

时间:2010-10-27 11:40:36

标签: apache .htaccess mod-rewrite codeigniter alias

我的网站上有一些.htaccess规则存在一个小问题 - http://www.presencemultimedia.co.uk

我们最近使用CodeIgniter重新构建了网站。为了使用漂亮的URL,我在.htaccess文件中添加了一些行,如下所示:

RewriteEngine on

# CodeIgniter rules (forwards requests to index.php)
RewriteCond $1 !^(index\.php|images|robots\.txt|public)
RewriteRule ^(.*)$ /index.php/$1 [L]

# rewrites path to our primary domain (www.presencemultimedia.co.uk)
RewriteCond %{http_host} !www.presencemultimedia.co.uk$
RewriteRule ^(.*)$ http://www.presencemultimedia.co.uk/$1 [r=301,nc]

第二条重写规则旨在确保域名始终是我们的主要域名(www.presencemultimedia.co.uk)。

我遇到的问题是,如果网站是由别名域访问的,例如http://www.prmulti.com,将URL重写到主域,但将/index.php/添加到路径中。

例如 - http://www.prmulti.com/about/应重写为http://www.presencemultimedia.co.uk/about/而不是http://www.presencemultimedia.co.uk/index.php/about

谁能看到我出错的地方?

干杯,菲尔

2 个答案:

答案 0 :(得分:3)

我很满意这个配置:

RewriteEngine on

# rewrites path to our primary domain (www.presencemultimedia.co.uk)
RewriteCond %{http_host} !www.presencemultimedia.co.uk$
RewriteRule ^(.*)$ http://www.presencemultimedia.co.uk/$1 [r=301,nc,l]

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

如果域规则匹配,则重定向(但将其作为最后一条规则)。 然后在正确的域上,如果请求不是现有文件或目录,则传递给Code Igniter。

答案 1 :(得分:2)

我原以为301重定向应该高于你的CodeIgniter规则,给出:

RewriteEngine on

# rewrites path to our primary domain (www.presencemultimedia.co.uk)
RewriteCond %{http_host} !www.presencemultimedia.co.uk$
RewriteRule ^(.*)$ http://www.presencemultimedia.co.uk/$1 [r=301,nc]

# CodeIgniter rules (forwards requests to index.php)
RewriteCond $1 !^(index\.php|images|robots\.txt|public)
RewriteRule ^(.*)$ /index.php/$1 [L]

我最近开发了一个网站的多站点框架,这就是我正在使用的重写顺序:首先是域名,然后通过我的index.php引导结果。对于像CodeIgniter这样的现成框架,理论应该是相同的。