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]