Home > database >  Redirect stripping the matched string - Htaccess
Redirect stripping the matched string - Htaccess

Time:06-10

Using .htaccess, I want to redirect all the pages which contains --nocolor- at the end of their url. I want redirect them to the same url without the match.

Example: https://www.test.com/products/test--nocolor- to https://www.test.com/products/test

What I've tried is

RewriteCond %{REQUEST_URI} (--nocolor-)

and that is working but I cannot get the right url while rewriting. I've tried

RewriteRule ^products/([^/]*)(?=--nocolor-) /$1 [R=301,L]
RewriteRule ^(. )(?=--nocolor-) /$1 [R=301,L]
RewriteRule ^.*(?=--nocolor-) /$1 [R=301,L]

but I always end up with $1 empty. What do I do wrong?

CodePudding user response:

With your shown samples, attempts please try following .htaccess rules. Make sure to place these rules at top of your .htaccess rules.

Also make sure to clear your browser cache before testing your URLs.

RewriteEngine ON
RewriteCond %{THE_REQUEST} \s/(.*)--nocolor-\s [NC]
RewriteRule ^ /%1 [R=301,L]
  • Related