.htaccess 重定向 301:多个斜杠的问题
.htaccess redirect 301: issue with multiple slashes
我必须在我的网站上进行一些重定向。我去年从 WP 迁移到 Jekyll,所以目录发生了变化,特别是关于图像位置和类别。
首先,我从 http 重定向到 https。然后,从 www 到非 www。然后,我删除 index.html
。然后,我删除了多个斜杠:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+(.*?)/{2,}([^\s]*)
RewriteRule ^ %1/%2 [R=301,L]
</ifModule>
最后,我做了一些特定的 301 重定向。比如这个:
Redirect 301 /wp-content/uploads/2012 /
结果是 domain/2012
而不是 domain
...
如果我尝试这样做:
Redirect 301 /wp-content/uploads/2012/ /
结果是域//
我该如何解决这个问题?这是正确的吗?
# Redirect HTTP to HTTPS
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>
# Remove www subdomain
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/ [R=301,L]
</IfModule>
# Remove index.html
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.html$ / [R=301,L]
RewriteRule ^(.*)/index\.html$ // [R=301,L]
</ifModule>
# Remove multiple slashes
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+(.*?)/{2,}([^\s]*)
RewriteRule ^ %1/%2 [R=301,L]
</ifModule>
您可以在站点根目录 .htaccess 中尝试这些规则:
RewriteEngine On
## add https, remove www and index.html in same rule
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTPS} !on [OR]
RewriteCond %{THE_REQUEST} /index\.html[?\s] [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^(.*?)(?:index\.html)?$ http://%1/ [L,R=301,NE,NC]
# remove multiple slashes from URL
RewriteCond %{THE_REQUEST} \s[^?]*//
RewriteRule ^.*$ /[=10=] [R=301,L,NE]
# specific 301 redirects with optional trailing slash
RewriteRule ^wp-content/uploads/2012/?$ /? [L,R=301]
确保在本地 Apache 上测试此 .htaccess 之前完全清除浏览器缓存。
我必须在我的网站上进行一些重定向。我去年从 WP 迁移到 Jekyll,所以目录发生了变化,特别是关于图像位置和类别。
首先,我从 http 重定向到 https。然后,从 www 到非 www。然后,我删除 index.html
。然后,我删除了多个斜杠:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+(.*?)/{2,}([^\s]*)
RewriteRule ^ %1/%2 [R=301,L]
</ifModule>
最后,我做了一些特定的 301 重定向。比如这个:
Redirect 301 /wp-content/uploads/2012 /
结果是 domain/2012
而不是 domain
...
如果我尝试这样做:
Redirect 301 /wp-content/uploads/2012/ /
结果是域//
我该如何解决这个问题?这是正确的吗?
# Redirect HTTP to HTTPS
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>
# Remove www subdomain
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/ [R=301,L]
</IfModule>
# Remove index.html
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.html$ / [R=301,L]
RewriteRule ^(.*)/index\.html$ // [R=301,L]
</ifModule>
# Remove multiple slashes
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+(.*?)/{2,}([^\s]*)
RewriteRule ^ %1/%2 [R=301,L]
</ifModule>
您可以在站点根目录 .htaccess 中尝试这些规则:
RewriteEngine On
## add https, remove www and index.html in same rule
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTPS} !on [OR]
RewriteCond %{THE_REQUEST} /index\.html[?\s] [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^(.*?)(?:index\.html)?$ http://%1/ [L,R=301,NE,NC]
# remove multiple slashes from URL
RewriteCond %{THE_REQUEST} \s[^?]*//
RewriteRule ^.*$ /[=10=] [R=301,L,NE]
# specific 301 redirects with optional trailing slash
RewriteRule ^wp-content/uploads/2012/?$ /? [L,R=301]
确保在本地 Apache 上测试此 .htaccess 之前完全清除浏览器缓存。