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)
CodePudding user response:
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.
RewriteEngine ON
RewriteCond %{THE_REQUEST} !\s/(?:test/one/|user/name/)\S \s [NC]
RewriteRule ^ - [F,L]
CodePudding user response:
There's no need for a RewriteCond
, just give the pattern to the RewriteRule
, e.g.
RewriteRule ^test/one - [F]
RewriteRule ^user/name - [F]
This will forbid requests, starting with either /test/one
or /user/name
.
To rewrite all other requests to some web page, append another RewriteRule
RewriteRule ^ /some/web/page.html [L]
To redirect the client instead of rewriting, add flag R|redirect to the rule
RewriteRule ^ /some/web/page.html [R,L]