Search code examples
.htaccesshttp-redirectmod-alias

htaccess redirect subdirectories to upper level


What rule can I add .htaccess that would give me the following result?

any subfolder that include "/manufacturer/any symbols" in url just rewrite to upper level, for example

domain.net/catalog/file/manufacturer/?categorylayout=0&showcategory=1&showproducts=1&productsublayout=0

to

domain.net/catalog/file/

Htaccess

RedirectMatch 301 ^/manufacturer/ / 

wont work. It should remove GET-parameters from end of url, and QSD-flag causes 500 error.


Solution

  • I think this is what you are looking for:

    RewriteEngine on
    RewriteRule ^/?(.+)/manufacturer/ /$1/ [R=301,QSD]
    

    This will work in the http servers host configuration or in dynamic configuration files (.htaccess) if the rewriting module is loaded.

    The interpretation of dynamic configuration files has to be enabled to if you decide to use such. In this case that file should be placed in your http servers DocumentRoot folder.


    Your description contradicts itself in a small details: in the description you say "/manufacturer/any symbols", but the example shows a URL without such symbols (note that the GET parameters are not considered part of the URL). So the above rule might have to be modified slightly depending on what your precise requirement is...


    And a general hint: you should always prefer to place such rules inside the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those files are notoriously error prone, hard to debug and they really slow down the server. They are only provided as a last option for situations where you do not have control over the host configuration (read: really cheap hosting service providers) or if you have an application that relies on writing its own rewrite rules (which is an obvious security nightmare).