我想将所有人重定向到我的https://站点(而不是https://www),并在一次重定向中强制使用HTTPS。我与.htaccess相关的部分:
# Check if HTTPS and WWW
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [OR,NC]
RewriteCond %{https} off
# Force HTTPS and remove WWW
RewriteRule ^(.*)/$ https://example.com/$1 [R=301,L]
虽然它适用于主页(例如http://www.example.com重定向到https://example.com),但不适用于子页面(例如http://www.example.com/contact应当将我重定向到{{ 3}},但是只有www被删除,我最终进入了非https网站:https://example.com/contact)。
如何更改我的.htaccess规则,以强制HTTPS并以单重定向方式删除我的网站以及网站的所有页面的WWW?
由于Nisarg和http://example.com/contact的回答,我的网站现在即使在子页面上也可以正确重定向到HTTPS。耶!
现在,相关的.htaccess代码如下所示:
RewriteEngine On
RewriteCond %{HTTPS} =off [OR]
RewriteCond %{HTTP_HOST} !^example\.com$
RewriteRule ^(.*)$ "https://example.com/$1" [R=301,L]
# remaining htaccess mod_rewrite CODE for WordPress
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
不幸的是,它需要2个步骤来处理。因此,问题仍然存在:可以在1个重定向中完成吗?
答案 0 :(得分:0)
尝试以下操作:(假设您正在使用index.php
)
RewriteEngine On
# Redirect to www
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
#
RewriteRule ^ - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
#
# If the requested path and file is not /index.php and the request
# has not already been internally rewritten to the index.php script
RewriteCond %{REQUEST_URI} !^/index\.php
# and the requested path and file doesn't directly match a physical file
RewriteCond %{REQUEST_FILENAME} !-f
# and the requested path and file doesn't directly match a physical folder
RewriteCond %{REQUEST_FILENAME} !-d
# internally rewrite the request to the index.php script
RewriteRule ^ index.php [L]
在测试此更改之前,请确保清除浏览器缓存。