开发者

Mod Rewrite Question

开发者 https://www.devze.com 2023-01-30 19:51 出处:网络
Okay its been a while since I used mod rewrite and I created a couple of mod rewrites along time ago and forgot what they did and I was wondering what exactly does this code snippet do. Can someone pl

Okay its been a while since I used mod rewrite and I created a couple of mod rewrites along time ago and forgot what they did and I was wondering what exactly does this code snippet do. Can someone please be as detailed as possible as to what the snippet does. Thanks!

Here is my mod rewrite code.

RewriteRule ^/?sitemap.xml?$ sitemap.php [L,N开发者_StackOverflow社区C,QSA]


Basically anything that looks for sitemap.xml will be passed to sitemap.php but without it showing to the user, that is, the url doesn't change for the user. Here is some of the documentation:

Taken from the Apache mod_rewrite Flags:

RewriteRule pattern target [Flag1,Flag2,Flag3]

L|last

The [L] flag causes mod_rewrite to stop processing the rule set. In most contexts, this means that if the rule matches, no further rules will be processed.

If you are using RewriteRule in either .htaccess files or in sections, it is important to have some understanding of how the rules are processed. The simplified form of this is that once the rules have been processed, the rewritten request is handed back to the URL parsing engine to do what it may with it. It is possible that as the rewritten request is handled, the .htaccess file or section may be encountered again, and thus the ruleset may be run again from the start. Most commonly this will happen if one of the rules causes a redirect - either internal or external - causing the request process to start over.

It is therefore important, if you are using RewriteRule directives in one of these context that you take explicit steps to avoid rules looping, and not count solely on the [L] flag to terminate execution of a series of rules, as shown below.

The example given here will rewrite any request to index.php, giving the original request as a query string argument to index.php, however, if the request is already for index.php, this rule will be skipped.

RewriteCond %{REQUEST_URI} !index\.php RewriteRule ^(.*) index.php?req=$1 [L]

NC|nocase

Use of the [NC] flag causes the RewriteRule to be matched in a case-insensitive manner. That is, it doesn't care whether letters appear as upper-case or lower-case in the matched URI.

In the example below, any request for an image file will be proxied to your dedicated image server. The match is case-insensitive, so that .jpg and .JPG files are both acceptable, for example.

RewriteRule (.*\.(jpg|gif|png))$ http://images.example.com$1 [P,NC]

QSA|qsappend

When the replacement URI contains a query string, the default behavior of RewriteRule is to discard the existing query string, and replace it with the newly generated one. Using the [QSA] flag causes the query strings to be combined.

Consider the following rule:

RewriteRule /pages/(.+) /page.php?page=$1 [QSA]

With the [QSA] flag, a request for /pages/123?one=two will be mapped to /page.php?page=123&one=two. Without the [QSA] flag, that same request will be mapped to /page.php?page=123 - that is, the existing query string will be discarded.


When "sitemap.xm" is requested redirect the request to "sitemap.php" instead. "L" means leave (or skip any following rules)

NOTE
QSA flag has to do with Query String handling (combine the old and new). I couldn't find anything about NC.

The reference has the full detail (Apache).

0

精彩评论

暂无评论...
验证码 换一张
取 消