1

So I've been trying this for a long time, but I can't seem to get it to work. Pretend my website is https://example.com, with the additional pages https://example.com/abc and https://example.com/abc-def-g. The plan is for all pages, both those that exist and those that don't, to automatically redirect to the homepage, with the exception of the two pages listed above.

Everything I've tried has failed, either preventing me from accessing the website or just doing nothing at all, so I'm trying to start fresh on a blank slate.

This is the .htaccess file at the moment:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
# BEGIN EWWWIO
# END EWWWIO

This is the standard file for WordPress. Any help would be much appreciated!

2
  • Have you considered doing this in PHP instead? HTAccess only works on Apache servers, and requires a less widespread skillset to answer than PHP
    – Tom J Nowell
    Aug 29, 2017 at 23:19
  • I haven't, mostly because as I was looking online, basically everyone was saying to use .htaccess, so I was thinking it was the right way to go. I'll give PHP a go though!
    – N.Hyatt
    Aug 30, 2017 at 12:57

1 Answer 1

1

Try the following, before the existing WordPress directives:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule !^(abc|abc-def-g)$ / [R,L]

This states that for any URL that is not /abc or /abc-def-g and which does not map to a physical file or directory (ie. your static resources, images, CSS, JS, etc.) then redirect to the homepage.

The ! prefix on the RewriteRule pattern negates the regex.

This is a temporary (302) redirect.

You don't need to repeat the RewriteEngine On directive.


UPDATE: If your URLs end in a trailing slash then you should modify the RewriteRule pattern:

RewriteRule !^(abc|abc-def-g)/$ / [R,L]

Or, make the trailing slash optional:

RewriteRule !^(abc|abc-def-g)/?$ / [R,L]

Another way of writing the same thing is (as you mentioned in comments) use RewriteCond directives to check the REQUEST_URI server variable instead of the RewriteRule pattern. This might be easier to read, however, it is marginally less efficient since the RewriteRule pattern is always processed first. For example:

RewriteCond %{REQUEST_URI} !^/abc/?$
RewriteCond %{REQUEST_URI} !^/abc-def-g/?$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ / [R,L]

Which is the same as (using alternation):

RewriteCond %{REQUEST_URI} !^/(abc|abc-def-g)/?$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ / [R,L]
3
  • Trying this, it does redirect back to the homepage, but both pages, abc and abc-def-g also redirect back.
    – N.Hyatt
    Aug 30, 2017 at 12:57
  • Actually, I got it to work. I added RewriteCond %{REQUEST_URI} !^/abc/?$ RewriteCond %{REQUEST_URI} !^/abc-def-g/?$ after the first two lines.
    – N.Hyatt
    Aug 30, 2017 at 13:06
  • If that works then it would seem your URLs end in a trailing slash, unlike your example? I've updated my answer. Using the RewriteCond directive to check against the REQUEST_URI is another way of doing essentially the same thing - except it is marginally less efficient.
    – MrWhite
    Aug 30, 2017 at 13:14

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.