Search code examples
apache.htaccesshttp-redirectmod-rewriteurl-rewriting

Redirect stripping the matched string - Htaccess


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?


Solution

  • 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]