Server Fault is a question and answer site for system and network administrators. 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 have hundreds of dynamically created URLs -,, etc.

I'd like to redirect all those URLs (don't know all of them, just know there are a-z or 1-9) to just

Please let me know as to how this can be done using apache rewrite rules

I have tried : RewriteRule ^(.*)$ index.php

But that does not work.

share|improve this question
up vote 0 down vote accepted

As answered by Brian above:

RewriteCond %{REQUEST_URI} !index.php
RewriteRule ^(.*)$ /index.php [R=301]

As suggested by Chris above:

RewriteCond %{REQUEST_URI} !index.php
RewriteRule ^(.*)$ /index.php [R=307]

If you are trying to do an internal redirect, where the url is NOT rewritten in the browser:

RewriteCond %{REQUEST_URI} !index.php
RewriteRule ^(.*)$ /index.php

Brian's method will do a 301 redirect. This will tell any search engine/browser/cache that the content has moved permanently, the existing url is gone and shouldn't be used. Chris's method will do a 307 redirect. 307 is the HTTP/1.1 equivalent of a temporary redirect. Both will rewrite the URL in the browser, both will have the effect of the person going to the site, clicking that url and being redirected to the new url of The last method will do that rewrite of the url internally so that the url in the browser remains rather than

None of the rules actually pass any parameters to index.php, so, the 301 & 307 redirects would actually lose the remainder of the url. If you are using the 301/307 redirects to redirect everything to a doorway page, this behavior might be acceptable. If that is your intention, I would suggest using:

ErrorDocument 404 /index.php

and then make sure all images/links are relatively/absolutely linked in that page or you use a to make sure urls are properly rewritten on the resulting page.

If you want to pass the requested URI to index.php as a parameter for parsing:

RewriteCond %{REQUEST_URI} !index.php
RewriteRule ^(.*)$ /index.php?page=$1

In this case, $_REQUEST['page'] would contain /contact from

share|improve this answer

Your rewrite rule will cause an infinite loop, because index.php also matches ^(.*)$. You should be able to do it with something like this (not tested):

RewriteCond %{REQUEST_URI} !index.php
RewriteRule ^(.*)$ /index.php [R=301]

The additional [R=301] will return a "permanently moved" redirect status code.

share|improve this answer
He should probably use [R=307], as he probably doesn't want those other URIs to be marked invalid (by RFC compliant browsers and search engines). – Chris S Jun 23 '10 at 15:50

Based on his url structure, he probably doesn't want the [R=301] as he's trying to make pretty urls.

share|improve this answer
If you're commenting on an answer please leave it as a comment to that specific answer. If your comment has too much information to leave in an actual comment, please at least directly reference (link) the answer you're commenting on. – Chris S Jun 23 '10 at 15:51
As I have only 36 rep, I am unable to comment on answers. You might find helpful, scroll down to the part regarding required reputation points. – karmawhore Jun 23 '10 at 16:15

Your Answer


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

Not the answer you're looking for? Browse other questions tagged or ask your own question.