Home > Mobile >  apache .htaccess redirect regex
apache .htaccess redirect regex

Time:01-22

I need this redirect 301 rule in apache .htaccess:

/catalog/{category_slug}/{product_slug} 

to

/product/{product_slug}

example:

/catalog/cat1/prod1 to /product/prod1
/catalog/cat1/prod2 to /product/prod2
/catalog/cat2/prod3 to /product/prod3

Could you please help me?

tks Simone

CodePudding user response:

At the top of your root .htaccess file using mod_rewrite:

RewriteEngine On

RewriteRule ^catalog/[^/] /([^/] )/?$ /product/$1 [R=302,L]

The $1 backreference contains the "{product-slug}" (3rd path segment) from the capturing group in the preceding RewriteRule pattern.

CodePudding user response:

Add this to the top of your .htaccess file:

RewriteEngine On # no need to do this twice, once is enough

RewriteRule ^catalog/[^/]*/(.*) /product/$1 [R=302,QSA,L]

Make sure mod_rewrite is enabled on the server.

This will redirect:

  1. /catalog/abc/xyz to /product/xyz
  2. /catalog/abc/123/xyz to /product/123/xyz
  3. Also redirect with the query string because of QSA flag.
  •  Tags:  
  • Related