Tell me more ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

I used to have a rewrite rule to

RewriteRule ^([^/]+)\.htm$ index.php?c=$1 [NC]
RewriteRule ^([^/]+)\.htm/([0-9.]+)$ index.php?c=$1&amt=$2 [NC]

Now, I have to change to

RewriteRule ^1/([^/]+)\.htm$ index.php?c=$1 [NC]
RewriteRule ^1/([^/]+)\.htm/([0-9.]+)$ index.php?c=$1&amt=$2 [NC]
RewriteRule ^2/([^/]+)\.htm$ index2.php?c=$1 [NC]
RewriteRule ^2/([^/]+)\.htm/([0-9.]+)$ index2.php?c=$1&amt=$2 [NC]

The difference is adding a sub-directory.

My question is, how can I redirect my old links to the new 1/ sub-directory?

share|improve this question
You mean index.php is located in the /1 subdirectory? – Jason McCreary May 21 '11 at 13:53
index.php is in the root directory, but i have two now(index.php, index2.php), and need to redirect the old ones to sub-directory 1/. index.php can be any other name, right? – user761396 May 21 '11 at 13:58

migrated from stackoverflow.com May 21 '11 at 21:14

3 Answers

If i was understand you right

 RewriteRule ^([^/]+)\.htm$ /1/index.php?c=$1 [NC]
 RewriteRule ^([^/]+)\.htm/([0-9.]+)$ /1/index.php?c=$1&amt=$2 [NC]
 RewriteRule ^1/([^/]+)\.htm$ index.php?c=$1 [NC]
 RewriteRule ^1/([^/]+)\.htm/([0-9.]+)$ index.php?c=$1&amt=$2 [NC]
 RewriteRule ^2/([^/]+)\.htm$ index2.php?c=$1 [NC]
 RewriteRule ^2/([^/]+)\.htm/([0-9.]+)$ index2.php?c=$1&amt=$2 [NC]
share|improve this answer
no, i don't have a /1/index.php. the problems is /aaa.htm is still seeing in search engine, but with the new rule. /aaa.htm changed to /1/aaa,htm after the new rewrite. i need a new rule to fix search engine seeing the old wrong not existing any more. – user761396 May 21 '11 at 14:07

If you want to redirect traffic from search engines, you should use 301 Redirect using something like:

RewriteRule ^([^12][^/]+)\.htm$ 1/index.php?c=$1 [R=301,L]

This will not work if some of your old files start by 1 or 2. If so, you can probably omit the [^12] and place this rule last. You will have to tag the old rules with last as well, replacing the [NC] by [NC,L].

share|improve this answer
1  
This looks right except I think it would be more appropriate to redirect to 1/$1.htm – matthew Jul 21 '11 at 4:04
Actually, you can just omit the [^12], period. The regexp ^([^/]+)\.htm$ cannot match anything beginning with 1/ or 2/, since those prefixes contain a slash. – Ilmari Karonen Mar 17 '12 at 18:58

Itai's answer is going in the right direction, but I don't think it's quite there yet. This is how I would do it:

RewriteRule ^([^/]+\.htm(/[0-9.]+)?)$ 1/$1 [NC,NS,L,R=301]

This will match those (and only those) URLs matched by your original two RewriteRules, and will prefix 1/ to them using an HTTP 301 redirect.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.