htaccess重定向不正常

时间:2015-02-12 10:03:55

标签: .htaccess redirect

我的网页网址应以https://www开头。我的一些重定向无法正常工作。我将提出的例子是:

romancestuck.com/aboutus/amycunningham.htm

应该重定向到:

https://www.romancestuck.com/aboutus.htm

但它会陷入重定向循环。

我的.htaccess文件中的代码是:

RewriteEngine On
RewriteBase /

### REDIRECT NON-WWW TO WWW - START ###
  RewriteCond %{HTTP_HOST} !^www\.
  RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301]
### REDIRECT NON-WWW TO WWW - END ###

### REDIRECT NON-HTTPS TO HTTPS - START ###
  RewriteCond %{HTTPS} off
  RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301]
### REDIRECT NON-HTTPS TO HTTPS - END ###

redirect 301 /aboutus/amycunningham.htm https://www.romancestuck.com/aboutus.htm

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

重构你的规则:

RewriteEngine On

RewriteRule ^aboutus/amycunningham\.htm$ https://www.romancestuck.com/aboutus.htm [L,NC,R=302]

### REDIRECT NON-WWW TO WWW & REDIRECT NON-HTTPS TO HTTPS ###
RewriteCond %{HTTP_HOST} !^www\. [OR]
RewriteCond %{HTTPS} off
RewriteRule ^ https://www.romancestuck.com%{REQUEST_URI} [R=302,L,NE]

请记住在测试之前在新浏览器中测试或清除浏览器缓存。

验证一切正常后,将R=302替换为R=301。在测试mod_rewrite规则时,请避免使用R=301(永久重定向)。

相关问题