重定向不适用于复杂的重写规则(.htaccess)

时间:2010-10-23 20:01:17

标签: apache .htaccess mod-rewrite

我正在尝试从非www版本的链接重定向到www。它适用于像http://mywebsite.com这样的工作,但它对http://mywebsite.com/artists/metallica/之类的请求甚至是复杂的请求都失败了。整个.htaccess文件如下。有线索吗?

Options +FollowSymLinks

RewriteEngine On

RewriteBase / 

RewriteCond %{REQUEST_FILENAME} !-f 

RewriteCond %{REQUEST_URI} !(.*).html

RewriteCond %{REQUEST_URI} !(.*)/$ 

RewriteRule ^(.*)$ http://mywebsite.com/$1/ [L,R=301]

RewriteRule ^artists/([^/-]+)-p([^/]+)/$ /artists.php?l=$1&p=$2 [QSA,L] 

RewriteRule ^artists/([^/]+)/$ /artists.php?l=$1 [QSA,L] 

RewriteRule ^submit/$ /submit.php [QSA,L] 

RewriteRule ^users/$ /users.php [QSA,L]  

RewriteCond %{THE_REQUEST} ^.*/index.php 

RewriteRule ^(.*)index.php$ http://www.mywebsite.com/$1 [R=301,L]

1 个答案:

答案 0 :(得分:1)

尝试此规则:

RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

但请确保将此规则放在那些只进行内部重写的规则之前。否则,内部重写的规则可能会被外部重定向。