Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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

Possible Duplicate:
Everything You Ever Wanted to Know about Mod_Rewrite Rules but Were Afraid to Ask?

What do the following lines mean? I am sorry, I am a new learner of regular expressions.

RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteCond %1/%{REQUEST_FILENAME} -f
RewriteCond %1/%{REQUEST_FILENAME} -d
RewriteRule index.php?=%{REQUEST_FILENAME} [L]

After reading some Apache rewrite documentation, I now have some basic knowledge, but I still can't understand some part of the above lines well.

%{HTTP_HOST} the requset domain

[NC] (case insensitive)

L (last - stop processing rules)

-f if Something_to_test is a file

-d if Something_to_test is a directory

%{REQUEST_FILENAME} the requset file name

  1. What does the $ mean in (^www\.(.*)$). All the URLs that are requested by the client are all beginning by www. . Am I right?

  2. What's the meaning %1/%?

  3. RewriteRule index.php?=%{REQUEST_FILENAME}

If the three RewriteConds are all right, it will execute the RewriteRule line. What does the rule mean?

share|improve this question

migrated from Jul 30 '11 at 10:05

This question came from our site for professional and enthusiast programmers.

marked as duplicate by jscott, Tom O'Connor, SmallClanger, sysadmin1138 Jul 30 '11 at 11:41

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

This isn't perl, this is just apache configuration. – Daenyth Jul 30 '11 at 3:35
that looks like a .htaccess did you try googling any of the key words. Like RewriteCond – austinbv Jul 30 '11 at 3:39
austinbv, have read some tutorial, but still don't know some part of those lines. could you explain it to me? – zhuanzhou Jul 30 '11 at 8:00

These lines are not regular expressions but mod_rewrite directives.

To understand what this ruleset does, you need to know how a ruleset is processed internalls and what the directives mean. For the latter you should check the corresponding description in the manual.

Doing that, you should see that the RewriteRule is probably not how it was intended. Because the first argument (i.e. index.php?=%{REQUEST_FILENAME}) is interpreted as regular expression pattern and the second as substitution (i.e. [L]). And that doesn’t make sense. It should probably read something like this:

RewriteRule ^ index.php?=%{REQUEST_FILENAME} [L]

So I’m assuming the following ruleset instead:

RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteCond %1/%{REQUEST_FILENAME} -f
RewriteCond %1/%{REQUEST_FILENAME} -d
RewriteRule ^ index.php?=%{REQUEST_FILENAME} [L]

This ruleset can be interpreted as follows:


  • the pattern ^ matches the given request URI path
  • and ^www\.(.*)$ matches the value of HTTP_HOST
  • and the match of the first subpattern (i.e. %1) of the previous successful RewriteCond pattern matching concatenated with / and the value of REQUEST_FILENAME can be mapped onto an existing regular file (i.e. -f)
  • and can also be mapped onto an existing directory (i.e. -d),

then rewrite the request internally to index.php?=%{REQUEST_FILENAME} and make this the last rule in the current processing (i.e. L flag).

But this rule is nonsense. Because a request cannot be mapped on both an existing regular file and an existing directory at the same time. Furthermore, REQUEST_FILENAME is an absolute filesystem path; prepending something like a domain suffix to it will make it invalid.

share|improve this answer

The rules rewrite an HTTP GET to use a query string for PHP.

For example, it turns 

share|improve this answer
could you explain the line one by one. thank you. – zhuanzhou Jul 30 '11 at 3:49
@zhuanzhou: you really ought to just read the documentation and figure out how it works yourself. We aren't going to do everything for you. If you come to an understanding of .htaccess rewrite rules and still have a question about how some part of it works, then you can come back. – siride Jul 30 '11 at 5:38
siride, i have read some tutorial, but still don't know some part of those lines. could you explain it to me? – zhuanzhou Jul 30 '11 at 7:53