13 Practical Apache rewrite rewrite rules _linux

Source: Internet
Author: User
Tags sub domain

1. Remove the WWW tag from the domain name

Copy Code code as follows:
Rewritecond%{http_host}!^jb51\.net$ [NC]
Rewriterule.? Http://jb51.net%{request_uri} [R=301,l]

2. Remove the WWW tag, but save the child domain name
Copy Code code as follows:
Rewritecond%{http_host} ^www\. (([A-z0-9_]+\.)? jb51\.net) $ [NC]
Rewriterule.? Http://%1%{request_uri} [R=301,l]

Here, when the 1% variable is matched, the subdomain is crawled in%2 (the internal atom), and we need exactly this%1 variable.
3. Add www tag to sub domain
Copy Code code as follows:
Rewritecond%{http_host} ^ ([A-Z.] +)? jb51\.net$ [NC]
Rewritecond%{http_host}!^www\. [NC]
Rewriterule.? Http://www.%1jb51.net%{request_uri} [R=301,l]

This rule captures the%1 variable of level two domain, and if it is not started with www, then add www, the previous domain name and {Request_uri} will follow.
4. Prevent picture hotlinking
Some webmasters will do anything to hotlinking your pictures on their website, consuming your bandwidth. You can add code to stop this behavior.
Copy Code code as follows:
Rewritecond%{http_referer}!^$
Rewritecond%{http_referer}!^http://(www\.)? jb51\.net/[NC]
Rewriterule \. (gif|jpg|png) $–[f]

If the {http_referer} value is not empty, or if it is not from your own domain name, this rule uses [F]flag to block URLs ending with gif|jpg|png
If you are adamant about this hotlinking, you can also change the image so that users visiting the Hotlinking site know that the site is stealing your pictures.
Copy Code code as follows:
Rewritecond%{http_referer}!^$
Rewritecond%{http_referer}!^http://(www\.)? jb51\.net/.*$ [NC]
Rewriterule \. (gif|jpg|png) $ your picture address [r=301,l]

In addition to blocking pictures hotlinking links, the above rules replace all of their hotlinking pictures with the pictures you set.
You can also prevent specific domain names from hotlinking your picture:
Copy Code code as follows:
Rewritecond%{http_referer}!^http://(www\.)? leech_site\.net/[NC]
Rewriterule \. (gif|jpg|png) $–[f,l]

This rule will block all picture link requests on the domain name blacklist.
Of course, these rules are based on the {http_referer} to get the domain name, if you want to switch to IP address, with {REMOTE_ADDR} on it.
5. If the file does not exist redirect to 404 page
If your host does not provide a 404 page redirect service, then we create it ourselves.
Copy Code code as follows:
Rewritecond%{request_filename}!-f
Rewritecond%{request_filename}!-d
Rewriterule.? /404.php [L]

Here-F matches the existence of the file name,-D matches the existence of the path name. This code will determine whether your file name and pathname exist before you make a 404 redirect. You can also add one on the 404 page? Url=$1 parameters:
Copy Code code as follows:
Rewriterule ^/? (. *) $/404.php?url=$1 [L]

In this way, your 404 pages can do some other things, such as default confidence, send an email reminder, add a search, and so on.
6. Renaming a directory
If you want to rename a directory on your site, try this:
Copy Code code as follows:
Rewriterule ^/?old_directory/([a-z/.] +) $ new_directory/$1 [r=301,l]

In the rules, I added a "." (Note that all characters are not represented, preceded by an escape character) to match the file's suffix name.
7. Converts the. html suffix name to. php
The premise is that the. html file can continue to access the case, update your site link.
Copy Code code as follows:
Rewriterule ^/? ([a-z/]+) \.html$ $1.php [L]

This is not a page redirection, so visitors are not visible. Let him as a permanent redirect (visible), will flag modify [r=301,l].
8. Create a link without a file suffix name
If you want to make the links to your PHP site simpler and easier to remember-or hide the file's suffix name, try this:
Copy Code code as follows:
Rewriterule ^/? ([a-z]+) $ $1.php [L]

If your site is mixed with PHP and HTML files, you can use Rewritecond to determine if the file for that suffix exists, and then replace it:
Copy Code code as follows:
Rewritecond%{request_filename}.php-f
Rewriterule ^/? ([a-za-z0-9]+) $ $1.php [L]
Rewritecond%{request_filename}.html-f
Rewriterule ^/? ([a-za-z0-9]+) $ $1.html [L]

If the file is a. php suffix, this rule will be executed.
9. Examine specific parameters in a query variable
If you have a special parameter in the URL, you can use Rewritecond to identify whether it exists:
Copy Code code as follows:
Rewritecond%{query_string}!uniquekey=
Rewriterule ^/?script_that_requires_uniquekey\.php$ other_script.php [qsa,l]

The rules above will check for the existence of the UniqueKey parameter in {query_string}, and if the {Request_uri} value is Script_that_requires_uniquekey, it will be directed to the new URL.
10. Delete query variable
The Apache Mod_rewrite module automatically identifies the query variable unless you make the following changes:
a). Assign a new query parameter (you can save the original query variable with [Qsa,l]flag)]
b). Add a "?" after the filename. (e.g. index.php?). Symbol "?" is not displayed in the browser's address bar.
11. Displays the current URI in a new format
If this is the urls:/index.php?id=nnnn that we are currently running. We really want to change it to/nnnn and let the search engine show up in a new format. First, we have to redirect the old URLs to the new format in order for the search engine to be new, but we also have to make sure that the previous index.php can still run. Are you confused by me?
The trick is to add a tag "marker" to the query variable that a visitor cannot see. We redirect only the links in the query variable that do not appear in the "marker" tag, and then replace the original chain replacement with a new one, and add a "marker" tag to the existing argument by [Qsa]flag]. The following are the ways to implement:
Copy Code code as follows:
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.jb51.net/index.php?id=nnnn, which does not contain marker, is permanently redirected to http://www.jb51.net/nnnn by the first rule, and the second rule http:// WWW.JB51.NET/NNNN to http://www.jb51.net/index.php?marker&id=nnnn and added marker and id=nnnn two variables, and finally Mod_ Rewrite begins the process of processing.
Second match, marker is matched, so ignore the first rule, here is a "." The character appears in the http://www.jb51.net/index.php?marker&id=nnnn, so the second rule is ignored, so we're done.
Note that this solution requires some extended functionality of Apache, so if your site is placed on a shared host you will encounter many obstacles.
12. Ensure security Service is enabled
Apache can distinguish between {HTTPS} and {Server_port} variables by using two ways to tell if you are on security services:
Copy Code code as follows:
Rewritecond%{request_uri} ^secure_page\.php$
Rewritecond%{https}!on
Rewriterule ^/? (secure_page\.php) $ https://www.jb51.net/$1 [r=301,l]

The above rule tests whether the {Request_uri} value equals our secure page code, and {HTTPS} is not equal to on. If both conditions are met, the request is redirected to the Security Service URI. In addition, you can use {Server_port} to do the same test, 443 is a common Security service port
Copy Code code as follows:
Rewritecond%{request_uri} ^secure_page\.php$
Rewritecond%{server_port}!^443$
Rewriterule ^/? (secure_page\.php) $ https://www.jb51.net/$1 [r=301,l]

13. Enforcing security services on a specific page
There is a security service domain name and a unsecured service domain name in the same server root directory, so you need to use Rewritecond to determine if the security service port is occupied, and only the following list of page requirements for security services:
Copy Code code as follows:
Rewritecond%{server_port}!^443$
Rewriterule ^/? (PAGE1|PAGE2|PAGE3|PAGE4|PAGE5) $ https://www.jb51.net/%1[r=301,l]

Here's how to return a page that is not set up as a security service to port 80:
Copy Code code as follows:
Rewritecond%{Server_port} ^443$
Rewriterule!^/? (PAGE6|PAGE7|PAGE8|PAGE9) $http://www.jb51.net%{request_uri} [R=301,l]

In fact, the most used in rewrite should also be regular expression, if the understanding of a few regular words, write this rule is relatively simple.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.