如何将多个网址重定向到主页的单个网址?

时间:2010-09-14 08:51:43

标签: php .htaccess codeigniter redirect

我有一个建立在codeigniter上的网站,但我遇到了一个小问题。基本上可以通过3个不同的网址访问主页:

www.domain.com/en

www.domain.com/en /

www.domain.com/en/home.html

我希望第一个和第三个网址重定向到第二个网址(www.domain.com/en /)。

我一直在玩.htaccess几个小时,我似乎无法做任何改变。

任何帮助都会非常感激。

修改 这是我目前的.htaccess

Options +FollowSymLinks
RewriteEngine on
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond $1 !^(index\.php|images|css|js|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [QSA,L]

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

RewriteRule ^en(/home.html)?$ /en/ [L]

3 个答案:

答案 0 :(得分:2)

RewriteRule ^en(/home.html)?$ /en/ [L]

这只匹配/ en和/en/home.html,没有其他网址。

答案 1 :(得分:1)

您当前的解决方案无效,因为RewriteRule ^(.*)$ /index.php/$1 [QSA,L] The L flag denotes last, meaning that no further rules should be processed.

已经捕获了任何输入
Options +FollowSymLinks
RewriteEngine on
RewriteBase /

#Everything from here has to do with the line mentioned above
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond $1 !^(index\.php|images|css|js|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [QSA,L] # This catches all input

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

RewriteRule ^en(/home.html)?$ /en/ [L]

相反,您想要处理的任何内容都需要在该行之前向上移动:

Options +FollowSymLinks
RewriteEngine on
RewriteBase /


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

RewriteRule ^en(/home.html)?$ /en/ [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond $1 !^(index\.php|images|css|js|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [QSA,L]

除此之外,阅读mod_rewriteregular expressions不能在这里受伤。

答案 2 :(得分:0)

可以像更改

一样简单

^en(/home.html)?$ /en/ [L]

^/en(/home.html)?$ /en/ [L](注意额外的斜线)

相关问题