首页 > 解决方案 > .htaccess 将整个站点从非 www/http 重定向到 www/https

问题描述

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

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

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 现在反映了这篇文章中的答案(将非 www 和非 http 重定向到 https)并且还包括整个 .htaccess 文件内容。

我正在通过使用 putty 开始会话然后按照@StephenOstermiller 的建议使用 (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 - 不确定这是否相关?

标签: .htaccessredirectcanonical-link

解决方案


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

使用该[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]

推荐阅读