admin管理员组文章数量:1208155
So here is the full .htaccess file, with code added to redirect all urls/pages to the only accessible page of the site, except content, includes and admin ones:
# 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.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
# BEGIN Custom
<IfModule mod_rewrite.c>
## Redirect all except given page/url to given website address
## src:
RewriteCond %{REQUEST_URI} !^/$
RewriteCond %{REQUEST_URI} !^/page-accessible/
RewriteCond %{REQUEST_URI} !^/wp-content/
RewriteCond %{REQUEST_URI} !^/wp-admin/
RewriteCond %{REQUEST_URI} !^/wp-includes/
RewriteRule .* /page-accessible/ [L,R=301]
</IfModule>
# END Custom
But it only redirects root.
Can someone please explain? Is there a better way to do this?
Thank you
So here is the full .htaccess file, with code added to redirect all urls/pages to the only accessible page of the site, except content, includes and admin ones:
# 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.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
# BEGIN Custom
<IfModule mod_rewrite.c>
## Redirect all except given page/url to given website address
## src: https://stackoverflow.com/questions/54176472/htaccess-redirect-all-links-except-base-url
RewriteCond %{REQUEST_URI} !^/$
RewriteCond %{REQUEST_URI} !^/page-accessible/
RewriteCond %{REQUEST_URI} !^/wp-content/
RewriteCond %{REQUEST_URI} !^/wp-admin/
RewriteCond %{REQUEST_URI} !^/wp-includes/
RewriteRule .* https://molhokwai-tests.cloudaccess.host/page-accessible/ [L,R=301]
</IfModule>
# END Custom
But it only redirects root.
Can someone please explain? Is there a better way to do this?
Thank you
Share Improve this question asked Dec 18, 2021 at 13:54 Mayou NkensaMayou Nkensa 111 bronze badge1 Answer
Reset to default 1Your rules are in the wrong order. Your "Custom Redirect" needs to be at the top of the .htaccess
file before the WordPress front-controller, ie. before the # BEGIN WordPress
comment marker.
By placing it after the WordPress code block it will only get processed for requests that map directly to files and directories (including the document root, but excluding /index.php
). (Although your custom redirect actually excludes the root, by the first condition?)
UPDATE:
but
RewriteCond %{REQUEST_URI} !^/path/page-accessible/
is ignored, so the page is also redirected... ?
Well, it's not "ignored". However, the request is then internally rewritten to index.php
(the WP front-controller) by the later rule and it's this that is then redirected on the second pass through the rewrite engine (which makes it look as if the above condition is being ignored).
You need to either:
Exclude
index.php
by adding another condtion to your rule. For example:RewriteCond %{REQUEST_URI} !^/index\.php$ :
OR,
Make sure the rule is only applied to direct requests from the client and not internally rewritten requests by the later rewrite. For example:
RewriteCond %{ENV:REDIRECT_STATUS} ^$ :
The
REDIRECT_STATUS
environment variable is empty on the initial request and set to "200" (as in 200 OK HTTP response status) after the later rewrite.
本文标签: redirecthtaccess rules not applied
版权声明:本文标题:redirect - htaccess rules not applied? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738737297a2109642.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论