【问题标题】:301 redirect for index.html and all http,https, www and non-www version of homepage URL to https://www root url via htaccess通过 htaccess 将 index.html 和所有 http、https、www 和非 www 版本的主页 URL 重定向到 https://www 根 URL 的 301
【发布时间】:2021-03-23 00:15:48
【问题描述】:

我想通过 htaccess 文件对以下 URL 进行 301 重定向

这意味着我需要强制使用 SSL(https) 版本并在整个网站上强制使用 www。

另外我还想将index.html 301重定向到https和www版本的首页url。

我想301重定向以下网址:

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.example.com”

除此之外,我还需要将网站的所有 url 重定向到 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 重定向 2 个带有 /index.html 的 url 和 301 的其余部分。

它给出一个 302 来自:

“https://www.example.com/index.html”

到这里

“https://www.example.com/“

谢谢。

【问题讨论】:

  • 欢迎来到 SO。请添加明确的 URL 详细信息示例,例如您希望从哪个 url 重定向/重写的 url,方法是将它们包装在 CODE TAGS 中,以便更好地理解您的问题。
  • 对不起,我会编辑这个
  • 对不起,我在移动设备上,没有完整的功能。
  • 评论不代表问题/努力,如前所述,请更新您的问题,谢谢。
  • 谢谢。现在完成。唷

标签: .htaccess ssl redirect


【解决方案1】:

您可以在一条规则中完成所有这些操作:

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]

【讨论】:

  • 谢谢,我也去看看这个。
  • 抱歉,但这不会将 index.html 重定向到 根 url。
【解决方案2】:

好吧,我自己想通了:

这是它的工作原理,只需要在最后一条规则中添加 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]

【讨论】:

    猜你喜欢
    • 2014-12-16
    • 2013-07-12
    • 2017-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-12
    • 2018-09-26
    相关资源
    最近更新 更多