重定向301不能正常工作的codeigniter

时间:2014-02-11 06:16:58

标签: php .htaccess redirect

我有一个地方的两个链接。

链接1:http://www.myhappyjourney.com/Rae%20Bareli 链接2:http://www.myhappyjourney.com/Rae-Bareli

我想永久地将链接1重定向到链接2。所有带空格的网址都用连字符。

尝试以下代码。但不行。它将index.php放在网址中。

RewriteCond %{THE_REQUEST} (\s|%20)
RewriteRule ^([^\s%20]+)(?:\s|%20)+([^\s%20]+)((?:\s|%20)+.*)$ $1-$2$3 [N,DPI]
RewriteRule ^([^\s%20]+)(?:\s|%20)+(.*)$ /$1-$2 [L,R=301,DPI]

注意:项目在codeigniter中。

完整.htaccess代码

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]

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

RewriteCond %{THE_REQUEST} (\s|%20)
RewriteRule ^([^\s%20]+)(?:\s|%20)+([^\s%20]+)((?:\s|%20)+.*)$ $1-$2$3 [N,DPI]
RewriteRule ^([^\s%20]+)(?:\s|%20)+(.*)$ /$1-$2 [L,R=301,DPI]

1 个答案:

答案 0 :(得分:2)

您可以将此规则用于空格连字符重定向:

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule "^(\S*)\s+(\S*\s.*)$" $1-$2 [N]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule "^(\S*)\s(\S*)$" /$1-$2 [L,R=301]

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

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]