htaccess 重定向无法正常工作
htaccess Redirects Not Working Right
我的网页 URL 应以 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}/ [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
如有任何帮助,我们将不胜感激!
像这样重构您的规则:
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
(永久重定向)。
我的网页 URL 应以 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}/ [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
如有任何帮助,我们将不胜感激!
像这样重构您的规则:
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
(永久重定向)。