Home > front end >  Using RewriteCond to match only a page or a directory having the same name
Using RewriteCond to match only a page or a directory having the same name

Time:09-17

I am trying to do a redirect of a file and folder using .htaccess. I want to add a RewriteCond directive before the RewriteRule to match only example.com/place/sta-ana.html and example.com/place/sta-ana directory. I tried this:

RewriteCond %{REQUEST_URI} ^place/sta-ana(\.html|/.*)?
RewriteRule ^place/sta-ana(\.html|/.*)? /place/santa-ana$1 [L,NC,R=301]

and this:

RewriteCond %{THE_REQUEST} ^place/sta-ana(\.html|/.*)?
RewriteRule ^place/sta-ana(\.html|/.*)? /place/santa-ana$1 [L,NC,R=301]

but both only work for redirecting place/sta-ana.html and not the directory and its files like place/sta-ana/santiago.html and place/sta-ana/santiago/lourdes.html. How can I get the code(s) above to also redirect the sta-ana directory to the santa-ana folder:

example.com/place/sta-ana.html => example.com/place/santa-ana.html
example.com/place/sta-ana/santiago.html => example.com/place/santa-ana/santiago.html
example.com/place/sta-ana/santiago/lourdes.html => example.com/place/santa-ana/santiago/lourdes.html

CodePudding user response:

Looks like you don't need a RewriteCond. Just this rule will do the job:

RewriteRule ^place/sta-ana(\.html|/.*)$ /place/santa-ana$1 [L,NC,R=301,NE]

Make sure this is your topmost rule and you must clear your browser cache completely before testing this change.

  • Related