13 Practical Apache Rewrite rewrite rules

Source: Internet
Author: User
Tags subdomain

1. Remove the WWW tag from the domain name

The code is 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 subdomain

The code is 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 fetched in %2(internal Atom), which is exactly what we need for this %1 variable.

3. Add www tags to subdomains

The code is 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 fetches the %1 variable of level two domain name, if not start with www , then add www, previous domain name and {request_ URI} will follow after.

4. Prevent picture hotlinking

Some webmasters are unscrupulous in hotlinking your pictures on their website, consuming your bandwidth. You can add code to block this behavior.

The code is 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 is not from your own domain name, this rule is blocked with [F]flag ] gif|jpg|png End of URL

If you are determined to despise this hotlinking, you can also change the picture so that users who visit the hotlinking site know that the site is stealing your pictures.

The code is 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 the picture hotlinking link, the above rule replaces its hotlinking image with the picture you set.

You can also block specific domain names from hotlinking your images :

The code is as follows :

Rewritecond%{http_referer}!^http://(www\.)? leech_site\.net/[NC]

Rewriterule \. (gif|jpg|png) $ – [f,l]

This rule will block all image link requests on the domain name blacklist.

Of course these rules are based on {http_referer} to get the domain name, if you want to use the IP address, with {remote_addr} You can do it.

5. Redirect to 404 page if file does not exist

If your host does not provide the 404 page redirection service, then we create it ourselves.

The code is as follows :

Rewritecond%{request_filename}!-f

Rewritecond%{request_filename}!-d

Rewriterule.? /404.php [L]

Here- F matches the existence of the file name, and-D matches the existence of the path name. This code will determine whether your file name and pathname exist before 404 redirects. You can also Add a . url=$1 parameter to the 404 page:

The code is as follows :

Rewriterule ^/? (. *) $/404.php?url=$1 [L]

This way, your 404 page can do some other things, such as default confidence, send an email alert, add a search, and so on.

6. Renaming a directory

If you want to rename the directory on the site, try this:

The code is as follows :

Rewriterule ^/?old_directory/([a-z/.] +) $ new_directory/$1 [r=301,l]

In the rules I added a ". "(Note that not all characters are represented, preceded by an escape character) to match the file's suffix name.

7. convert . html suffix names to . PHP

If the . HTML file can continue to be accessed, update your site link.

The code is as follows :

Rewriterule ^/? ([a-z/]+) \.html$ $1.php [L]

This is not a Web redirect, so the visitor is not visible. Let him as a permanent redirect (visible), change the FLAG to [r=301,l].

8. Create a no file suffix name link

If you want to make the link to your PHP site more concise and easy to remember-or to hide the file's suffix name, try this :

The code is as follows :

Rewriterule ^/? ([a-z]+) $ $1.php [L]

If the site is mixed with PHP and HTML files, you can use rewritecond First to determine if the suffix file exists, Then replace it with:

The code is 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 suffixed with . PHP , this rule will be executed.

9. Check for specific parameters in query variables

If There is a special parameter in the URL, you can use rewritecond to identify whether it exists:

The code is as follows :

Rewritecond%{query_string}!uniquekey=

Rewriterule ^/?script_that_requires_uniquekey\.php$ other_script.php [qsa,l]

The above rule will check if the uniquekey parameter inside {query_string} exists, if {Request_uri} value is Script_that_requires_uniquekey, it will bedirected to the new URL.

. Delete a query variable

Apache 's mod_rewrite module automatically recognizes query variables unless you make the following changes:

a). assign a new query parameter (you can use the [Qsa,l]flag Save the original query variable)

b). After the file name, add a " ? "(e.g. index.php? ). Symbol "? "will not be displayed in the browser's address bar.

One by one. Present the current URI in a new format

If this is the URLsWe are currently running:/index.php?id=nnnn. We very much want to change it to /nnnn and let the search engine show in the new format. First, we have to redirect the old URLs to the new format in order for the search engine to be updated, but we also have to make sure that the previous index.php still works. Did I get confused?

The trick is to add a tag "marker" that the visitor cannot see in the query variable. We redirect only the links in the query variable that do not appear in the "marker" tag, and then replace the original chain with the new format, and Add a "qsa]flag" to the existing parameter. marker"tag. Here's how it's implemented:

The code is 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, not included marker http://www.jb51.net/nnnn http://www.jb51.net/nnnn anti-redirect to http://www.jb51.net/index.php?marker&id=nnnn marker id=nnnn Two variables, last mod_rewrite

The second match,marker is matched, so ignore the first rule, here is a ". "character will appear 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 extensions to Apache, so if your site is placed on a shared host you will encounter many obstacles.

. Ensure security Service is enabled

There are two ways Apache can tell if you've turned on security services, referencing {HTTPS} and {server_port} variables , respectively:

The code is 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 {Server_port} do the same test, 443 is a common Security service port

The code is as follows :

Rewritecond%{request_uri} ^secure_page\.php$

Rewritecond%{server_port}!^443$

Rewriterule ^/? (secure_page\.php) $ https://www.jb51.net/$1 [r=301,l]

. Enforce security services on a specific page

There is a secure service domain name and a non-secure service domain name under the same server root, so you need to use Rewritecond to determine if the security service port is occupied and only the page requirements for the following list are security services:

The code is 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:

The code is 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 be the regular expression, if you know a little regular, write this rule is relatively simple.

13 Practical Apache Rewrite rewrite rules

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.