我知道這在這里被問了很多,但我仍然找不到適用于我的網站的答案。這些是我嘗試過的帖子/答案:
- .htaccess 從非 www http 子檔案夾重定向到 www https 子檔案夾
- 將非 www(http) 和 www(http) 重定向到 https://www
- htaccess 非 www 到 www 而 http 到 https 和 http://www 到 https://www
- 將非 www 重定向到 www 以獲取 https
- Htaccess 重定向:WWW HTTPS://WWW HTTP:// ---> HTTPS://non-www
- .htaccess 非 www 到 www 和 http 到 https 的重定向
- htaccess 重定向 https://www。(https://www.example.com) 到非 www (https://example.com)
- htaccess 從帶 https 的非 www 重定向到帶 https 的 www
- 還有很多...
我收到重復的內容錯誤,這是一個例子
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/
不僅在那個網址上,而且在整個網站上。我有一個指向正確版本的“規范”標簽設定,但我還想將所有網頁/目錄重定向到頁面的 www/https 版本 301。
這是我當前的 .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 檔案內容。
I am checking the headers by starting a session with putty and then using (curl --head http://example.co.uk) as suggested by @StephenOstermiller
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
I'm also using LetsEncypt - not sure if that's relevant?
uj5u.com熱心網友回復:
當問題是針對每個目錄的.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]
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/357521.html
