admin管理员组文章数量:1389882
I'm trying to improve my website SEO and several online tools have flagged that I need to ensure 301 redirects are configured on my website to
. Apparently, Google dings me if the redirects are confirgured properly. When I try to insert some lines of code to address this issue I get issues with my paths after the domain name.
Heres the code I have in my wordpress-vhost-config file. I assume I have some sort of conflict with rewrite rules but don't know what to adjust.
RewriteEngine On
RewriteRule ^bitnami/wordpress(/.*) $1 [L]
# END WordPress fix for plugins and themes
# BEGIN WordPress
#
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#this is the part that doesn't seem to work
#RewriteCond %{HTTP_HOST} !^www\.
#RewriteCond %{HTTPS}s on(s)|offs()
#RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [NE,L,R]
RewriteRule . /index.php [L]
I have inserted this code:
#this is the part that doesn't seem to work
#RewriteCond %{HTTP_HOST} !^www\.
#RewriteCond %{HTTPS}s on(s)|offs()
#RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [NE,L,R]
But have it commented it out for now as the URL paths were not working properly.
I'm trying to improve my website SEO and several online tools have flagged that I need to ensure 301 redirects are configured on my website https://example.co.uk
to https://www.example.co.uk
. Apparently, Google dings me if the redirects are confirgured properly. When I try to insert some lines of code to address this issue I get issues with my paths after the domain name.
Heres the code I have in my wordpress-vhost-config file. I assume I have some sort of conflict with rewrite rules but don't know what to adjust.
RewriteEngine On
RewriteRule ^bitnami/wordpress(/.*) $1 [L]
# END WordPress fix for plugins and themes
# BEGIN WordPress
# https://wordpress./support/article/htaccess/#basic-wp
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#this is the part that doesn't seem to work
#RewriteCond %{HTTP_HOST} !^www\.
#RewriteCond %{HTTPS}s on(s)|offs()
#RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [NE,L,R]
RewriteRule . /index.php [L]
I have inserted this code:
#this is the part that doesn't seem to work
#RewriteCond %{HTTP_HOST} !^www\.
#RewriteCond %{HTTPS}s on(s)|offs()
#RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [NE,L,R]
But have it commented it out for now as the URL paths were not working properly.
Share Improve this question edited Mar 17 at 0:32 MrWhite 46.1k8 gold badges64 silver badges88 bronze badges asked Mar 16 at 15:18 J. LayJ. Lay 232 bronze badges1 Answer
Reset to default 1You've inserted the code in the wrong place. A single "rule" consists of the RewriteRule
directive together with the preceding RewriteCond
directives (conditions). By placing the new rule where you have you've inserted it in the middle of the existing rule and consequently broken it.
Your new rule (canonical redirect) needs to go at the very top, immediately after the first RewriteEngine On
directive.
However, your new rule is not strictly correct for a couple of reasons:
- It preserves the requested protocol, either HTTP or HTTPS. It should redirect to HTTPS only.
- You have implemented a 302 (temporary) redirect. This is OK for testing, however, this ultimately needs to be a 301 (permanent) redirect.
In other words:
RewriteEngine On
# Redirect non-www to www (and HTTPS)
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [NE,R=301,L]
You should also probably implement an HTTP to HTTPS redirect for requests that are already to the correct hostname. This can be implemented as an additional rule following the one above (providing you are not implementing HSTS).
UPDATE: For example:
:
# HTTP to HTTPS redirect (following the rule above)
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,R=301,L]
Heres the code I have in my wordpress-vhost-config file.
Not sure exactly what you mean by this as this is non-standard. If these rules are being included directly in the <VirtualHost>
config (which is what this filename would seem to imply) then all these rules are wrong and it's going to fail horribly (RewriteBase
is not permited in a vhost context). So, I assume they are being included inside a <Directory>
container (which itself might be inside a <VirtualHost>
) and .htaccess
files are disabled? (Although you have tagged the question .htaccess
?)
Reference:
- https://httpd.apache./docs/2.4/mod/mod_rewrite.html
本文标签: htaccesshtaccess rewrite nonwww to httpswwwStack Overflow
版权声明:本文标题:.htaccess - htaccess rewrite: non-www to https:www - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744595630a2614759.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论