Apache .htaccess重定向301:多斜杠问题
我必须在我的网站上做一些重定向。去年我从WP迁移到了Jekyll,所以目录发生了变化,特别是关于图像位置和类别 首先,我从http重定向到https。然后,从www到非www。然后,我删除Apache .htaccess重定向301:多斜杠问题,apache,.htaccess,mod-rewrite,Apache,.htaccess,Mod Rewrite,我必须在我的网站上做一些重定向。去年我从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] <
index.html
。然后,我删除了多个斜杠:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+(.*?)/{2,}([^\s]*)
RewriteRule ^ %1/%2 [R=301,L]
</ifModule>
您可以在站点根目录中尝试以下规则。htaccess:
重新编写引擎打开
##添加https,删除同一规则中的www和index.html
重写cond%{HTTP_HOST}^www\。[北卡罗来纳州,或]
重写cond%{HTTPS}!在[或]
RewriteCond%{THE_REQUEST}/index\.html[?\s][NC]
重写cond%{HTTP_HOST}^(?:www\)?(.+)$[NC]
重写规则^(.*)(:index\.html)?$http://%1/$1[L,R=301,NE,NC]
#从URL中删除多个斜杠
RewriteCond%{THE_REQUEST}\s[^?]*//
重写规则^.*$/$0[R=301,L,NE]
#带有可选尾部斜杠的特定301重定向
重写规则^wp内容/上载/2012/?$/?[L,R=301]
在本地Apache上测试此.htaccess之前,请确保完全清除浏览器缓存。谢谢!这很有效。甚至我认为网站的加载时间更快,减少了TTFB。哎哟!12天后,谷歌搜索控制台返回“覆盖范围”上的“重定向错误”。我现在不确定该修什么。我想我修好了。对于https、www和index.html,我使用的是我的旧配置。对于多个斜杠和特定的301重定向,我使用您的代码。谢谢谢谢你的更新,很高兴知道你已经找到了解决办法。
# 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>