【问题标题】:.htaccess redirect entire site from non-www/http to www/https.htaccess 将整个站点从非 www/http 重定向到 www/https
【发布时间】:2021-12-24 10:31:39
【问题描述】:

我知道这个问题在这里被问了很多,但我仍然找不到适合我网站的答案。这些是我尝试过的帖子/答案:

出现重复的内容错误,这是一个示例

https://www.example.co.uk/coffee-machines/
http://example.co.uk/coffee-machines/
http://www.example.co.uk/coffee-machines/
http://example.co.uk/coffee-machines/

这些 URL 显示为重复的内容,所以我试图强制 301 重定向到

https://www.example.co.uk/coffee-machines/

不仅在该网址上,而且在整个网站上。我设置了一个指向正确版本的“规范”标签,但我还想将所有网页/目录 301 重定向到页面的 www/https 版本。

这是我当前的 .htaccess 内容

Options -Indexes

Options +FollowSymLinks

Options -MultiViews

AddDefaultCharset UTF-8

ErrorDocument 401 /401.php
ErrorDocument 404 /404.php

RewriteEngine on
RewriteBase /

RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]

这适用于将example.co.uk 重定向到 www/https 版本,但没有子页面。

有人可以为我指出正确的方向,将所有网址重定向到 www/https 版本的建议解决方案以停止重复内容。

我已将 .htaccess 更改为以下代码,以反映帖子被标记为重复。所以 .htaccess 现在反映了这篇文章 (Redirect non-www and non-http to https) 中的答案,并且还包含了整个 .htaccess 文件内容。

按照@StephenOstermiller 的建议,我通过使用 putty 开始会话然后使用 (curl --head http://example.co.uk) 来检查标题

Options -Indexes

Options +FollowSymLinks

Options -MultiViews

AddDefaultCharset UTF-8

ErrorDocument 401 /401.php
ErrorDocument 404 /404.php

RewriteEngine on
RewriteBase /

RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ https://www.xxx.co.uk%{REQUEST_URI} [NC,L,R=301,NE]

RewriteRule ^q&a/$ forum.php

RewriteRule ^q&a/(.*).html$ forumDetail.php?question=$1&%{QUERY_STRING} [B,L]

#RewriteRule ^q&a/(.*)$ /forum/$1 [R=301,NC,L]

RewriteRule ^blog/$ blog.php

RewriteRule ^blog/(.*).html$ blogDetail.php?blog_name=$1&%{QUERY_STRING} [B,L]

RewriteRule ^about-us/$ about.php
RewriteRule ^privacy-policy/$ privacy.php
RewriteRule ^disclaimer/$ disclaimer.php
RewriteRule ^my-account/$ user_account.php
RewriteRule ^activate-my-account/$ user_activate.php
RewriteRule ^update-my-account/$ user_change.php
RewriteRule ^login/$ user_login.php
RewriteRule ^logout/$ user_logout.php
RewriteRule ^register/$ user_register.php
RewriteRule ^reset-my-password/$ user_reset.php
RewriteRule ^home-energy/$ home-energy.php

RewriteRule ^compare/(.*)$ /kitchen-scales/$1 [R=301,NC,L]

RewriteRule ^kitchen_scales/(.*)$ /kitchen-scales/$1 [R=301,NC,L]
RewriteRule ^coffee_machines/(.*)$ /coffee-machines/$1 [R=301,NC,L]
RewriteRule ^digital_radios/(.*)$ /digital-radios/$1 [R=301,NC,L]
RewriteRule ^mixers_blenders/(.*)$ /mixers-blenders/$1 [R=301,NC,L]

RewriteRule ^4g-broadband-all-you-need-to-know.html$ 4g-broadband-all-you-need-to-know.php

Redirect 301 /kitchen-scales/category/digital-scales/index.php https://www.xxx.co.uk/kitchen-scales/category/digital-scales/
Redirect 301 /kitchen-scales/salter-kitchen-scales.html https://www.xxx.co.uk/kitchen-scales/brand/salter/
Redirect 301 /kitchen-scales/brand/index.php https://www.xxx.co.uk/kitchen-scales/brand/
Redirect 301 /kitchen-scales/brand/salter/index.php https://www.xxx.co.uk/kitchen-scales/brand/salter/

Redirect 301 /coffee-machines/category/bean-to-cup-coffee-machines/index.php https://www.xxx.co.uk/coffee-machines/category/bean-to-cup-coffee-machines/
Redirect 301 /coffee-machines/bean-to-cup-coffee-machines.html https://www.xxx.co.uk/coffee-machines/category/bean-to-cup-coffee-machines/
Redirect 301 /coffee-machines/bean-to-cup-coffee-machines.php https://www.xxx.co.uk/coffee-machines/category/bean-to-cup-coffee-machines/

<IfModule mod_headers.c>
  Header set Connection keep-alive
</IfModule>

<FilesMatch "\.(ico|pdf|jpg|jpeg|png|webp|gif|html|htm|xml|txt|xsl|css)$">
Header set Cache-Control "max-age=31536000"
</FilesMatch>

AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml text/css application/x-javascript application/javascript text/javascript

<FilesMatch "config\.php|config\.advanced\.php">
  Order allow,deny
  Deny from all
</FilesMatch>

<Files ~ "^.*\.([Hh][Tt][Aa])">
    Order allow,deny
    Deny from all
    Satisfy all
</Files>

AddType x-httpd-php73 .php

我也在使用 LetsEncypt - 不确定这是否相关?

【问题讨论】:

  • RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L] 执行 301 重定向到您网站的 http 版本,然后下一条规则将 http URL 重定向到 https,创建两个重定向。您可以只使用一个规则来强制执行 https 和 www。
  • @Stephen Ostermiller 对不起,你是对的,我的意思是主页重定向而不是子页面
  • 您当前拥有的规则不是来自您复制的任何其他问题,并且正如已经指出的重定向到 http。我知道您遇到了麻烦,但是您链接的答案确实有效。要记住的一件事是浏览器可以缓存 301 响应,因此在您更改 .htaccess 后,您还应该清除浏览器历史记录和/或重新启动浏览器,否则您看不到更改,我从经验中知道非常令人困惑和沮丧:-)
  • @Simon 好的,那么问题是您的 subdir/. htaccess 正在覆盖主 htaccess 文件。要解决此问题,只需将 ssl and non-www 规则放在您的 htaccess 顶部的子文件夹中 RewriteEngine on.
  • @AmitVerma 非常感谢您的帮助。那已经奏效了。我只是没有想到顶级 .htaccess 不会处理整个站点的重定向。如果您可以将其添加为答案,我会将其标记为已接受。我已经为此工作了很长时间,所以很高兴能解决它。再次感谢。

标签: .htaccess redirect canonical-link


【解决方案1】:

当问题是针对每个目录的.htaccess 文件时,您可以使用[END] 标志来阻止这些文件中的规则在某些规则之后运行。来自documentation

使用[END] 标志不仅会终止当前轮次的重写处理(如[L]),还会阻止任何后续的重写处理在每个目录(htaccess)上下文中发生。

所以你应该能够在你的规则中使用[END] 来使它们优先于子目录中的其他规则:

RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,END]

RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,END]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-01
    • 1970-01-01
    • 2018-11-12
    • 1970-01-01
    • 1970-01-01
    • 2015-06-15
    • 2014-07-26
    相关资源
    最近更新 更多