.htaccess 301将index.html和所有http、https、www和非www版本的主页URL重定向到https://www 通过htaccess访问根url
我想通过htaccess文件重定向以下URL 这意味着我需要强制SSL(https)版本,并强制整个网站使用www 此外,我还想将index.html重定向到主页url的https和www版本 我想301重定向以下URL:.htaccess 301将index.html和所有http、https、www和非www版本的主页URL重定向到https://www 通过htaccess访问根url,.htaccess,ssl,redirect,.htaccess,Ssl,Redirect,我想通过htaccess文件重定向以下URL 这意味着我需要强制SSL(https)版本,并强制整个网站使用www 此外,我还想将index.html重定向到主页url的https和www版本 我想301重定向以下URL: https://www.example.com/index.html https://example.com/index.html https://example.com http://www.example.com/index.html http://example.com
https://www.example.com/index.html
https://example.com/index.html
https://example.com
http://www.example.com/index.html
http://example.com/index.html
http://www.example.com
http://example.com
www.example.com
example.com
到
除此之外,我需要所有的网址的网站被重定向到https://www 类似这样的版本:
“https://www.example.com/new-site.html”
以下是我现在正在使用的:
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
RewriteCond %{HTTP_USER_AGENT} libwww-perl.*
RewriteRule .* ? [F,L]
RewriteBase /
RewriteRule ^index\.html$ / [NC,R,L]
但是上面的规则似乎给了我一个301--->302----200 ok重定向,用于带/index.html的两个url,其余的则是301
它给出了302,来自:
“https://www.example.com/index.html”
对此
“https://www.example.com/“
谢谢。好的,我自己想出来了: 下面是它的工作原理,只需要在最后一个规则中添加R=301,而不仅仅是R
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
RewriteCond %{HTTP_USER_AGENT} libwww-perl.*
RewriteRule .* ? [F,L]
RewriteBase /
RewriteRule ^index\.html$ / [NC,R=301,L]
您可以在一条规则中完成所有这些操作:
RewriteEngine On
## add www and turn on https in same rule
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} !on [OR]
RewriteCond %{REQUEST_URI} /index\.html$ [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule (^|.+/)(?:index\.html)?$ https://www.%1/$1 [R=301,L,NE]
欢迎来到SO。请添加清晰的url详细信息示例,例如从哪个url重定向/重写到哪个url,方法是将它们包装在代码标记中,以便更好地理解您的问题。抱歉,我将编辑此示例抱歉,我在移动中,没有完整的功能。评论并不表示问题/努力,如前所述,请更新您的问题,谢谢。谢谢。现在完成。谢谢你,我也会检查这个。对不起,这不会将index.html重定向到根url。是的,更新的版本可以工作
RewriteEngine On
## add www and turn on https in same rule
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} !on [OR]
RewriteCond %{REQUEST_URI} /index\.html$ [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule (^|.+/)(?:index\.html)?$ https://www.%1/$1 [R=301,L,NE]