首页 > 解决方案 > 如何在有和没有 www 的情况下从 http 重定向到 https 并具有规范的 url

问题描述

以前可能有人问过这个问题,但我只是想确保在我的情况下它是正确的。

基本上我在 PageChecking 界面上几乎没有错误,我可能有 2 个页面可用,这些页面可能会重复内容。目前我有来自基本 HTTP 的 HTTPS 重定向。

尽管我需要将 www 重定向到非 www https 并保留非 www 地址并添加 https 的重定向。

这就是我的 htaccess 的样子:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^website.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.website.com$


RewriteCond %{HTTPS} !=on
RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R,L]


RewriteCond %{HTTP_REFERER} ^http://www.ownedcore/upl/usr/ [NC]
RewriteCond %{HTTP_REFERER} ^http://www.bananaforum.com/ [NC]
RewriteCond %{HTTP_REFERER} ^http://www.dafont.com/[NC]
RewriteCond %{HTTP_REFERER} ^http://goingsony.com/ [NC]
RewriteCond %{HTTP_REFERER} ^http://i.imgur.com/ [NC]
RewriteCond %{HTTP_REFERER} ^http://www.wallpapervortex.com/ [NC]

RewriteRule . https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]


RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !web/
RewriteRule (.*) /web/$1 [L]

这些与其他 url 的 rewritecond 可能无关紧要,所以请让我知道我应该删除和修复什么,谢谢!

标签: .htaccessredirecthttp-status-code-301

解决方案


在 .htaccess 文件顶部检查此修改后的规则

RewriteEngine on

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

RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

RewriteCond %{HTTP_REFERER} ^http://www.ownedcore/upl/usr/ [NC]
RewriteCond %{HTTP_REFERER} ^http://www.bananaforum.com/ [NC]
RewriteCond %{HTTP_REFERER} ^http://www.dafont.com/[NC]
RewriteCond %{HTTP_REFERER} ^http://goingsony.com/ [NC]
RewriteCond %{HTTP_REFERER} ^http://i.imgur.com/ [NC]
RewriteCond %{HTTP_REFERER} ^http://www.wallpapervortex.com/ [NC]
RewriteRule . https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !web/
RewriteRule (.*) /web/$1 [L]

推荐阅读