joedborg
joedborg

Reputation: 18353

Write a .htaccess line to find and replace part of a URL

I know there are a few of these, but I can't find one that describes what I want to do.

I've got an page that I want to retire, for example:

www.mysite.com/pages/page_old.php?someargs

I'd like the _old to be stripped off, so the page goes to

www.mysite.com/pages/page.php?someargs

I've tried

RewriteEngine On
RewriteBase /
RewriteRule ^/pages/page_old.php$ ^/pages/page.php$ [L,R=301]

but it doesn't work.

Worth noting that ?someargs can be any args passed to the page, so this must be flexible.

Upvotes: 0

Views: 81

Answers (1)

Felipe Alameda A
Felipe Alameda A

Reputation: 11809

The leading slash is not at the URI-path tested in the rule, so it can't be in the regex either.

The substitution URL is a plain text string and regular expression characters shouldn't be used as they are passed literally.

You may try this instead:

RewriteRule ^pages/page_old.php  /pages/page.php [R=301,NC,L]

The query string will be passed through unchanged to the substitution URL

Upvotes: 1

Related Questions