Code
Http://skyfen.javaeye.com/category/94175? Show_full = true
1. Add WWW tags to subdomain names
Rewritecond % {http_host} ^ ([A-Z.] + )? Example \. com $ [Nc]
Rewritecond % {http_host }! ^ Www \. [Nc]
Rewriterule .? Http: // www.example.com % {request_uri} [R = 301, l]
This rule captures the % 1 variable of the second-level domain name. If it does not start with WWW, then WWW is added. The previous domain name and {request_uri} will follow it.
2. Remove the WWW mark from the domain name
Rewritecond % {http_host }! ^ Example \. com $ [Nc]
Rewriterule .? Http://example.com % {request_uri} [R = 301, l]
3. Remove the WWW tag, but save the sub-domain name
rewritecond % {http_host} ^ www \. ([a-z0-9 _] + \.)? Example \. com) $ [Nc]
rewriterule .? Http: // % 1% {request_uri} [R = 301, l]
here, after the 1% variable is matched, the subdomain name will be in % 2 (internal atom) and what we need is the % 1 variable.
4. Prevent image leeching
some Webmasters can steal your images on their websites at will, consuming your bandwidth. You can add Code to prevent such behavior.
rewritecond % {http_referer }! ^ $
rewritecond % {http_referer }! ^ Http: // (www \.)? Example \. COM/[Nc]
rewriterule \. (GIF | JPG | PNG) $-[f]
If the {http_referer} value is not blank or is not from your own domain name, this rule uses [f] flag to block URLs ending with GIF, JPG, and PNG.
If you despise this type of leeching, you can also change the image, let the users who access the leeching website know that the website is stealing your images.
rewritecond % {http_referer }! ^ $
rewritecond % {http_referer }! ^ Http: // (www \.)? Example \. COM /. * $ [Nc]
rewriterule \. (GIF | JPG | PNG) $ your image Address [R = 301, l]
besides blocking image leeching links, the above rules replace all the images with the ones you have set.
You can also prevent domain name leeching of your images:
rewritecond % {http_referer }! ^ Http: // (www \.)? Leech_site \. COM/[Nc]
rewriterule \. (GIF | JPG | PNG) $-[F, L]
this rule will block all image link requests on the domain name blacklist.
of course, the above rules are based on the {http_referer} domain name. If you want to use the IP address instead, use {remote_addr.
5. Redirect to 404 page if the file does not exist
If your host does not provide the 404 page redirection service, we will create it ourselves.
Rewritecond % {request_filename }! -F
Rewritecond % {request_filename }! -D
Rewriterule .? /404.php [l]
Here,-F matches the existing file name and-D matches the existing path name. Before you perform 404 redirection, this Code determines whether your file name and path name exist. You can also add one on the 404 page? Url = $1 parameter:
Rewriterule ^ /? (. *) $/404.php? Url = $1 [l]
In this way, you can do other things on the 404 page, such as default confidence, sending an email reminder, adding a search, and so on.
6. Rename the Directory
If you want to rename the directory on the website, try this:
Rewriterule ^ /? Old_directory/([A-z/.] +) $ new_directory/$1 [R = 301, l]
In the rule, I added a "." (note that it does not represent all characters and there is an escape character before it) to match the suffix of the file.
7.convert the suffix of .html to. php.
Previously, when the .html file can continue to be accessed, update your website link.
Rewriterule ^ /? ([A-z/] +) \. html $ 1.php [l]
This is not a webpage redirection, so visitors are invisible. Change the flag to [R = 301, l] as a permanent redirection (visible).
8. Create a link without a file suffix
If you want to make the link to your PHP website more concise and easy to remember-or hide the file suffix, try this:
Rewriterule ^ /? ([A-Z] +) $ 1.php [l]
If the website contains PHP and HTML files, you can use rewritecond to first determine whether the file with the suffix exists and then replace it:
Rewritecond % {request_filename}. php-F
Rewriterule ^ /? ([A-zA-Z0-9] +) $ 1.php [l]
Rewritecond implements request_filename}.html-F
Rewriterule ^ /? ([A-zA-Z0-9] +) $ 20.1.html [l]
If the file is suffixed with. php, this rule will be executed.
9. Check the specific parameters in the query variable.
If there is a special parameter in the URL, you can use rewritecond to identify whether it exists:
Rewritecond % {QUERY_STRING }! Uniquekey =
Rewriterule ^ /? Script_that_requires_uniquekey \. php $ other_script.php [QSA, l]
The above rules will check whether the uniquekey parameter in {QUERY_STRING} exists. If the {request_uri} value is script_that_requires_uniquekey, it will be directed to the new URL.
10. Delete query Variables
The mod_rewrite module of Apache automatically identifies the query variables unless you have made the following changes:
A). assign a new query parameter (you can use [QSA, l] flag to save the original query variable)
B) Add "?" to the end of the file name. (Such as index. php ?). Symbol "?" It is not displayed in the address bar of the browser.
11. display the current URI in a new format
If this is the currently running URL:/index. php? Id = NNNN. We would like to change it to/NNNN and display it in a new format. First, we have to redirect the old URLs to the new format to update the search engine, but we have to ensure that the previous index. php can still run. Am I confused?
To implement the above functions, the trick is to add a tag "marker" that the visitor cannot see in the query variable ". We only redirect the link without the "marker" mark in the query variable, and then replace the original link with the new format, the [QSA] flag is used to add a "marker" mark to an existing parameter. The following is an implementation method:
Rewritecond % {QUERY_STRING }! Marker
Rewritecond % {QUERY_STRING} id = (-a-zA-Z0-9 _ +] +)
Rewriterule ^ /? Index \. php $ % 1? [R = 301, l]
Rewriterule ^ /? ([-A-zA-Z0-9 _ +] +) $ index. php? Marker & ID = $1 [l]
Here, the original URL: http://www.example.com/index.php? Id = NNNN, which does not contain marker, so it is permanently redirected to slave by the first rule? Marker & ID = NNNN, and added the marker and ID = NNNN variables. Finally, mod_rewrite starts the processing.
The second match, marker is matched, so ignore the first rule, where there is a "." character that will appear in the http://www.example.com/index.php? Marker & ID = NNNN, so the second rule will be ignored, so we can complete it.
Note that this solution requires some extensions of Apache, so you will encounter many obstacles if your website is placed on a shared host.
12. ensure that the security service is enabled
Apache can identify whether the security service is enabled in two ways and reference the {HTTPS} and {server_port} variables respectively:
rewritecond % {request_uri} ^ secure_page \. PHP $
rewritecond % {HTTPS }! On
rewriterule ^ /? (Secure_page \. PHP) $ https://www.example.com/#1 [R = 301, l]
the above rule tests whether the value of {request_uri} is equal to our security Page code and {HTTPS} is not equal to on. If both conditions are met, the request will be redirected to the Security Service Uri. in addition, you can use {server_port} For the same test. 443 is a common security service port
rewritecond % {request_uri} ^ secure_page \. PHP $
rewritecond % {server_port }! ^ 443 $
rewriterule ^ /? (Secure_page \. php) $ https://www.example.com/#1 [R = 301, l]
13. Enforce security services on a specific page
When you encounter a security service domain name and a non-security service domain name under the root directory of the same server, you need to use rewritecond to determine whether the security service port is occupied, and only the following list of pages are required as security services:
Rewritecond % {server_port }! ^ 443 $
Rewriterule ^ /? (Page1 | page2 | page3 | page4 | page5) $ https://www.example.com/?1=r=301,l]
The following describes how to return a page that is not set as a security service to port 80:
Rewritecond % {server_port} ^ 443 $
Rewriterule! ^ /? (Page6 | page7 | page8 | page9) $ http://www.example.com % {request_uri} [R = 301, l]