I am trying to redirect any request that does not start with specific patterns.
For example
mydomain.com/test/one/a
mydomain.com/test/one/b
and
mydomain.com/user/name/a
mydomain.com/user/name/b
So any request that does not match with mydomain.com/test/one or mydomain.com/user/name will redirect to some page or web address.
So far i can match following
RewriteCond %{REQUEST_URI} ^(/test/one|/user/name) [NC]
RewriteRule .* - [F]
so any url started with /test/one or /user/name is redirecting properly.
But when i am trying to negate the condition with ! , its redirecting every requests!
RewriteCond %{REQUEST_URI} !^(/test/one|/user/name) [NC]
RewriteRule .* - [F]
But i want all request to redirect except REQUEST_URI has pattern like
^(/test/one|/user/name)
2
Answers
With your shown samples, please try following htaccess rules file. Please make sure to clear your browser cache before testing your URLs.
Using
THE_REQUEST
variable here for apache to check condition.There’s no need for a
RewriteCond
, just give the pattern to theRewriteRule
, e.g.This will forbid requests, starting with either
/test/one
or/user/name
.To rewrite all other requests to some web page, append another
RewriteRule
To redirect the client instead of rewriting, add flag R|redirect to the rule