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.