Search code examples
.htaccessurl-rewriting

How to redirect specific subdirectory name in htaccess


I need to redirect from:

http://example.com/*/this/any.html
http://example.com/*/*/this/any.html
http://example.com/*/*/*/this/any.html

to:

http://example.com/this/any.html

Solution

  • Try something like the following using mod_rewrite at the top of the root .htaccess file:

    RewriteEngine On
    
    RewriteRule ^(?:[^/]+/){1,3}(this/[\w-]+\.html)$ /$1 [R=302,L]
    

    any.html - the file basename (any) is permitted to contain the following characters: a-z, A-Z, 0-9, _ (underscore) and - (hypen)