我正在运行一个Wordpress博客,它与HTTPS重定向有问题。除了主页之外,如果您通过HTTP访问,则不会将其他URL重定向到HTTPS。
我想将所有HTTP流量重定向到HTTPS。目前,如果尝试使用HTTP,只有主页(http://www.example.com)被重定向到HTTPS。
但是如果您访问http://www.example.com/page1,那么它就不会被重定向到HTTPS并停留在HTTP上。
我不想使用任何插件,如“非常简单的SSL”。在网络上搜索了一下之后,我发现我可以修改.htaccess文件来做到这一点。然后,我试着理解.htaccess文件(考虑到我以前从未使用过PHP或WordPress,甚至是Apache )。我知道RewriteEngine On应该只在你的文件中出现一次,但在我的例子中,它出现了两次。也许一些插件或主题修改了这一点,我不知道。下面是我的.htaccess文件的内容。
# BEGIN WordPress
# The directives (lines) between `BEGIN WordPress` and `END WordPress` are
# dynamically generated, and should only be modified via WordPress filters.
# Any changes to the directives between these markers will be overwritten.
ExpiresActive On
ExpiresByType image/jpg "access plus 24 hours"
ExpiresByType image/jpeg "access plus 24 hours"
ExpiresByType image/gif "access plus 24 hours"
ExpiresByType image/png "access plus 24 hours"
ExpiresByType text/css "access plus 24 hours"
ExpiresByType application/pdf "access plus 1 week"
ExpiresByType text/javascript "access plus 24 hours"
ExpiresByType text/html "access plus 5 minutes"
ExpiresByType image/x-icon "access plus 1 year"
ExpiresDefault "access plus 24 hours"
Header set X-Endurance-Cache-Level "2"
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]我没有任何子域。只有一个域。因此,为了消除我的疑虑,我有两个问题。
1)这个.htaccess文件正确吗?我是说我们能有两条RewriteEngine On线路吗?那太外边了,再说一遍我对这个语法不太熟悉。
2)在我的所有页面上,我应该更改什么以反映HTTPS重定向?
Moreover,它还破坏了非WWW重定向。
PS:这些是我访问过的链接之一-
发布于 2020-05-23 16:45:03
您的HTTPS重写规则永远不会执行,因为它是在RewriteRule . /index.php [L]之后。该规则匹配所有的URL路径,并且是由于L标志而为它们执行的最后一条规则。
要使重定向规则工作,只需将其移动到.htaccess文件的顶部即可。
WordPress本身做了一些重定向。这里可能发生的情况是,您看到的重定向是由WordPress PHP代码完成的,而不是由.htaccess完成的。
发布于 2020-05-23 17:50:45
通过从@Stephen的答案中获得灵感,我将mod_rewrite块前面的最后3行移动来解决这个问题。
# BEGIN WordPress
# The directives (lines) between `BEGIN WordPress` and `END WordPress` are
# dynamically generated, and should only be modified via WordPress filters.
# Any changes to the directives between these markers will be overwritten.
ExpiresActive On
ExpiresByType image/jpg "access plus 24 hours"
ExpiresByType image/jpeg "access plus 24 hours"
ExpiresByType image/gif "access plus 24 hours"
ExpiresByType image/png "access plus 24 hours"
ExpiresByType text/css "access plus 24 hours"
ExpiresByType application/pdf "access plus 1 week"
ExpiresByType text/javascript "access plus 24 hours"
ExpiresByType text/html "access plus 5 minutes"
ExpiresByType image/x-icon "access plus 1 year"
ExpiresDefault "access plus 24 hours"
Header set X-Endurance-Cache-Level "2"
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPresshttps://webmasters.stackexchange.com/questions/129709
复制相似问题