admin管理员组文章数量:1302241
I want my WordPress website to load with https + non-www and without trailing slashes. I put the following code in .htaccess
file:
<IfModule mod_rewrite.c>
RewriteEngine On
# Remove trailing slash from non-filepath urls
RewriteCond %{REQUEST_URI} /(.+)/$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ /%1 [R=301,L]
# Force HTTPS and remove WWW
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [OR,NC]
RewriteCond %{https} off
RewriteRule ^(.*)$ /$1 [R=301,L]
</IfModule>
These rules work fine but I can't remove multiple slashes after the domain. The website loads:
/////
and I want it to redirect toAlso, these rules work if they are only in the beginning of the
.htaccess
file and when I re-save the permalinks the rules disappear...
Could you help me, please
I want my WordPress website to load with https + non-www and without trailing slashes. I put the following code in .htaccess
file:
<IfModule mod_rewrite.c>
RewriteEngine On
# Remove trailing slash from non-filepath urls
RewriteCond %{REQUEST_URI} /(.+)/$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ https://example/%1 [R=301,L]
# Force HTTPS and remove WWW
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [OR,NC]
RewriteCond %{https} off
RewriteRule ^(.*)$ https://example/$1 [R=301,L]
</IfModule>
These rules work fine but I can't remove multiple slashes after the domain. The website loads:
https://example/////
and I want it to redirect tohttps://example
Also, these rules work if they are only in the beginning of the
.htaccess
file and when I re-save the permalinks the rules disappear...
Could you help me, please
Share Improve this question edited Feb 26, 2021 at 16:42 MrWhite 3,8911 gold badge20 silver badges23 bronze badges asked Feb 26, 2021 at 14:53 x-alien23x-alien23 233 bronze badges2 Answers
Reset to default 2To reduce multiple slashes at the start of the URL-path (or in fact anywhere in the URL-path) to a single slash (there is always at least 1 slash at the start of the URL-path, even if you can't see this is the browser's address bar) then you can use a directive like the following:
# Reduce multiple slashes to single slashes
RewriteCond %{THE_REQUEST} \s[^?]*//
RewriteRule (.*) /$1 [R=301,L]
This uses the fact that the URL-path matched by the RewriteRule
pattern has already had multiple slashes reduced. The preceding condition checks that there were multiple slashes somewhere in the URL-path in the initial request.
You would place the above rule second, between your existing rules. And modify your 1st rule that removes the trailing slash to also reduce multiple slashes, thus preventing an unnecessary additional redirect should a request contain both multiple slashes and a trailing slash. For example:
# Remove trailing slash from non-directory URLs AND reduce multiple slashes
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.+)/$ https://example/$1 [R=301,L]
# Reduce multiple slashes to single slashes
RewriteCond %{THE_REQUEST} \s[^?]*//
RewriteRule (.*) https://example/$1 [R=301,L]
# Force HTTPS and remove WWW
RewriteCond %{HTTP_HOST} ^www\. [OR,NC]
RewriteCond %{https} off
RewriteRule (.*) https://example/$1 [R=301,L]
I just tidied the regex in the RewriteCond
directive that checks against the HTTP_HOST
server variable. ^www\.(.*)$
can be simplifed to ^www\.
since you are not making use of the backreference here.
You do not need the <IfModule>
wrapper.
Clear your browser cache before testing and preferably test first with 302 (temporary) redirects to avoid potential caching issues.
- Also, these rules work if they are only in the beginning of the .htaccess file and when I re-save the permalinks the rules disappear...
These rules certainly need to go near the beginning of the .htaccess
file, before the WordPress front-controller.
However, they must go before the # BEGIN WordPress
section. Not inside this block, otherwise they will be overwritten since WP itself maintains this code block.
They should not be overwritten if placed before the # BEGIN WordPress
comment marker.
to prevent WordPress from modifying your .htaccess, you can also do
add_filter('flush_rewrite_rules_hard', '__return_false');
A "hard" flush updates .htaccess (Apache) or web.config (IIS).
本文标签: mod rewritehtaccess redirects disappeared after resaving permalinks
版权声明:本文标题:mod rewrite - .htaccess redirects disappeared after re-saving permalinks 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741706815a2393610.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论