0

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?

2 Answers2

1

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:

If

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

Gumbo
  • 436
  • 2
  • 6
0

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

For example, it turns

domain.com/page/1/ 

into

domain.com?page=1
Peter Mortensen
  • 2,319
  • 5
  • 23
  • 24
austinbv
  • 143
  • 1
  • 5
  • 2
    @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. –  Jul 30 '11 at 05:38
  • siride, i have read some tutorial, but still don't know some part of those lines. could you explain it to me? –  Jul 30 '11 at 07:53