I'm trying to write a couple rules that will be dependent on the number of subdirectory levels present in a url. I have one rule that works for part of what I want, and another for the other part, but the cause conflicts with each other.
The rules and what they do:
This rule would take a URL like this: website.com/base/{WILDCARD}
and forward it to this: website.com/{WILDCARD}
RewriteRule ^base/(.*)$ /$1 [L,NC,R=301]
This rule would take a URL like this: website.com/base/{WILDCARD}/{WILDCARD2}
and forward it to this: website.com/{WILDCARD2}
RewriteRule ^base/([^/] )/(.*)$ /$2 [L,NC,R=301]
These rules should only go into effect if "base" is in the URL
any help is greatly appreciated!
CodePudding user response:
You can handle all these redirect by capturing last path component in URI:
RewriteEngine On
RewriteRule ^(?:procedures|gallery)/(?:. /)?([^/] /?)$ /$1 [L,NE,NC,R=302]
Once you verify it is working fine, replace R=302
to R=301
. Avoid using R=301
(Permanent Redirect) while testing your mod_rewrite
rules.
Here is a regex demo to demonstrate regex used above.