Tara Kelly
Tara Kelly

Reputation: 43

Remove string from any URL in a 301 Redirect

I have a bunch of old urls that all contain "qr_" in them. I would like to create a rule in the .htaccess file that would remove that string from all URLs which contain it.

Examples of desired output:

mydomain.com/qr_red --> mydomain.com/red
mydomain.com/qr_blue --> mydomain.com/blue
mydomain.com/qr_green --> mydomain.com/green

Would this code work?

RewriteCond %{REQUEST_URI} ^(.*/)?qr_(/.*)?$ [NC]
RewriteRule ^ %1%2 [L,R=301,NE]

I've based it off of this thread, but I'm hesitant to deploy it because instead of replacing one string with another, I'm replacing it with nothing (%1%2)? Not sure if that'll break something, or if there's a cleaner way to accomplish it.

Upvotes: 1

Views: 39

Answers (1)

anubhava
anubhava

Reputation: 785561

You may use this redirect rule:

RewriteRule ^qr_(.*) /$1 [L,R=301,NE,NC]

Upvotes: 1

Related Questions