Webmasters Stack Exchange is a question and answer site for pro webmasters. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?


migration rejected from stackoverflow.com Oct 17 '13 at 7:59

This question came from our site for professional and enthusiast programmers. Votes, comments, and answers are locked due to the question being closed here, but it may be eligible for editing and reopening on the site where it originated.

closed as off-topic by Stephen Ostermiller Oct 17 '13 at 7:59

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question appears to be off-topic because it is unlikely to help anybody else. Examples include individual website reviews for SEO purposes and general website troubleshooting." – Stephen Ostermiller
If this question can be reworded to fit the rules in the help center, please edit the 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

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.


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].

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