.htaccess重定向301:多个斜杠的问题



我必须在我的网站上进行一些重定向。去年我从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/$1 [R=301,L]
</IfModule>
# Remove index.html
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index.html$ / [R=301,L]
RewriteRule ^(.*)/index.html$ /$1/ [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/$1 [L,R=301,NE,NC]
# remove multiple slashes from URL
RewriteCond %{THE_REQUEST} s[^?]*//
RewriteRule ^.*$ /$0 [R=301,L,NE]
# specific 301 redirects with optional trailing slash
RewriteRule ^wp-content/uploads/2012/?$ /? [L,R=301]

在本地Apache上测试.htaccess之前,请确保完全清除浏览器缓存。

最新更新