chiaf
chiaf

Reputation:

.htaccess redirect after replace a word?

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?

Upvotes: 2

Views: 5441

Answers (3)

tsveti_iko
tsveti_iko

Reputation: 7952

This way you can redirect independent on what surrounds the 'oldword', which you want to replace:

<IfModule mod_rewrite.c>
   RewriteEngine on
   RewriteBase /

   # Replace 'oldword' with 'newword' and 301 redirect 
   RewriteCond %{REQUEST_FILENAME} !-d
   RewriteRule ^(.*)oldword(.*)$ /$1newword$2 [L,R=301]
</IfModule>

Upvotes: 0

3DH
3DH

Reputation: 1471

see here:

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteRule ^oldword(.*)$ http://%{HTTP_HOST}/newword$1 [L]
</IfModule>

ciao,

Chris

Upvotes: 1

zombat
zombat

Reputation: 94147

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]

Upvotes: 8

Related Questions