Ad

.htaccess Redirect After Replace A Word?

- 1 answer

I need to use .htaccess file to replace a word in a URL; something like this:

Example URL:

 http://example.com/oldword/test-page.html

Redirect to:

 http://example.com/newword/test-page.html

How can I use mod_rewrite to redirect every URL containing /oldword/ to the same URL after replacing that word?

Ad

Answer

This should do it for you:

RewriteRule ^oldword/(.*)   /newword/$1   [L]

Edit: It might not work exactly depending on your RewriteBase settings, but it'll be close.

Second Edit: If you need to have a 301 Moved Permanently header associated with the old URLs, you can do something like this as well:

RewriteRule ^oldword/(.*)   /newword/$1   [R=301,L]
Ad
source: stackoverflow.com
Ad