.htaccess 将整个站点从 non-www/http 重定向到 www/https

.htaccess redirect entire site from non-www/http to www/https

我知道这里有很多人问过这个问题,但我仍然找不到适合我网站的答案。这些是我试过的posts/answers:

我收到重复的内容错误,这是一个示例

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/

不仅在 url 上,而且在整个网站上。我设置了一个指向正确版本的“Canonical”标签,但我还想 301 将所有 Web pages/directories 重定向到页面的 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}/ [R=301,L]

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

这适用于将 example.co.uk 重定向到 www/https 版本但没有 sub-pages.

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

我已将 .htaccess 更改为以下代码以反映 post 被标记为重复。所以 .htaccess 现在反映了这个 post () 中的答案,并且还包含了整个 .htaccess 文件内容。

我正在检查 headers,方法是用腻子启动 session,然后按照 @StephenOstermiller[= 的建议使用 (curl --head http://example.co.uk) 27=]

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=&%{QUERY_STRING} [B,L]

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

RewriteRule ^blog/$ blog.php

RewriteRule ^blog/(.*).html$ blogDetail.php?blog_name=&%{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/ [R=301,NC,L]

RewriteRule ^kitchen_scales/(.*)$ /kitchen-scales/ [R=301,NC,L]
RewriteRule ^coffee_machines/(.*)$ /coffee-machines/ [R=301,NC,L]
RewriteRule ^digital_radios/(.*)$ /digital-radios/ [R=301,NC,L]
RewriteRule ^mixers_blenders/(.*)$ /mixers-blenders/ [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 - 不确定这是否相关?

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

Using the [END] flag terminates not only the current round of rewrite processing (like [L]) but also prevents any subsequent rewrite processing from occurring in per-directory (htaccess) context.

因此您应该能够在您的规则中使用 [END] 使它们优先于子目录中的其他规则:

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

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