How do I create a rewrite rule that only matches a request with no query string?


should remap to:




Should be left alone.

I'm trying this:

RewriteRule ^/index.php$   http://www.mysite.com/ [R=301,L]

...But it's matching anything that starts with /index.php even though I've explicitly ended the pattern with a dollar sign.


The only rules before this one are:

 RewriteCond %{HTTP_HOST}   ^mysite.com [NC]
 RewriteRule ^/(.*)         http://www.mysite.com/$1 [R=301,L]

The intention is to redirect non www links to the www version.

  • This should work as expected, the syntax is fine. Are you including this as the first rule in your chain of rewrites? Is there a conflicting rewrite elsewhere (in httpd.conf vs .htaccess, etc?) Perhaps something is short-circuiting the rules somewhere and this is never reached. Just some thoughts. Nov 13, 2009 at 0:10
  • I've updated the original question to include the two rules before this one.
    – Nick
    Nov 13, 2009 at 0:22

4 Answers 4


The [L] flag on the first set of rules here:

RewriteCond %{HTTP_HOST}   ^mysite.com [NC]
RewriteRule ^/(.*)         http://www.mysite.com/$1 [R=301,L]

L means Last -- Apache will stop processing rules for this iteration when it is reached.

Remove the L flag and you should be fine.

  • It appears to me that this rewrite rule isn't the problem. The poster's site runs on the www subdomain and this rule only executes when that is not the case. It is not preventing his other rewrite rules from working. May 21, 2014 at 9:31

Here's the solution I came up with:

RewriteCond %{QUERY_STRING} ^$
RewriteRule ^/index.php$   / [R=301,L]

RewriteCond ! %{QUERY_STRING} would match requests without a query string.

You can also add the following to turn on logging and debugging of mod_rewrite RewriteLog logs/rewrite_log RewriteLogLevel 3

Be aware that this can slow you site down and fill up your logs so only use it for debugging.

  • 1
    It didn't work- the condition didn't seem to match anything, so the rule never ran. This worked though: RewriteCond %{QUERY_STRING} ^$
    – Nick
    Nov 13, 2009 at 10:24

All these rewrites are RegExps which are quite slow. It should be a bit faster if you do it from index.php:

if (!strlen($_SERVER["QUERY_STRING"])) header("Location: /");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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