Home > front end >  Redirection from after slash word to query
Redirection from after slash word to query

Time:05-02

I am very new in coding and redirection in .htaccess. I would need a redirection for a lot of URLs with the slug /brand/. For example:

https://example.com/brand/AAA to /shop/?filter_marke=AAA
https://example.com/brand/BBB to /shop/?filter_marke=BBB
https://example.com/brand/CCC to /shop/?filter_marke=CCC

and so on.

CodePudding user response:

You could perform the "redirect" like the following using mod_rewrite:

RewriteEngine On

RewriteRule ^brand/(\w )$ /shop/?filter_marke=$1 [R=302,L]

The order of rules can be important. This would need to go near the top of the .htaccess file, before any existing rewrites. If this is a WordPress site, then it would need to go before the # BEGIN WordPress comment marker.

The $1 backreference in the substitution string (2nd argument) contains the value of the word after /brand/ in the URL-path.


UPDATE:

I only forgot to mention that there is a slash after the variable, means the incoming link looks like ..../AAA/

In that case you can simply append a trailing slash to the end of the pattern, ie. ^brand/(\w )/$. Or make the trailing slash optional so it matches both. ie. ^brand/(\w )/?$.

  • Related