1

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

1 Answer 1

1

Your 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.

2
  • Hello @MrWhite - Thanks - I've put the rules before the Wordpress section. Now other rules are correctly enforced, but RewriteCond %{REQUEST_URI} !^/path/page-accessible/ is ignored, so the page is also redirected... ? Feb 8, 2022 at 17:15
  • @MayouNkensa Ah, the request is being rewritten to index.php and it's this that is actually redirected. You need to add another condition (RewriteCond directive) - I've updated my answer.
    – MrWhite
    Feb 8, 2022 at 19:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.