MikeE
MikeE

Reputation: 1

301 Redirect Rule

Need help with an 301 htaccess redirect rule doing the following:

www.name.com/wordA/wordB/* to www.name.com/WordNEW/wordA/wordB/*

we are basically adding "WordNew".

I have three wordA and five wordB for a total of 15 path variations.

Upvotes: 0

Views: 200

Answers (3)

Gumbo
Gumbo

Reputation: 655129

Here’s a simpler rule:

RewriteRule !^WordNEW/ /WordNEW%{REQUEST_URI} [L,R=301]

Upvotes: 0

random
random

Reputation: 9955

Spin this on for size:

RewriteEngine On
RewriteCond %{REQUEST_URI} !^/WordNEW [NC]
RewriteRule ^(.*)$ /WordNEW/$1 [L,R=301]

Broken down

RewriteCond %{REQUEST_URI} !^/WordNEW [NC]

Check the requested URI does not start (!^) with the same folder path (/WordNEW) as the final. If it does, you've already redirected once or you are already there. If you don't check then you can end up on a loop rewriting the path over and over again.

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

If it passes, grab the whole requested URI (^(.*)$) and prepend with the new folder path (/WordNEW/$1). Then flag this as the last rule (L) in the RewriteRule while redirecting under 301 (R=301).

Upvotes: 1

Woot4Moo
Woot4Moo

Reputation: 24316

if WordNEW is in a constant position my quick and dirty solution would be to split the url string on '/'. at index 1 I would prepend the string WordNEW/ . For a more feasible solution I will need a few moments to think.

Upvotes: 0

Related Questions