. htaccess

Source: Internet
Author: User
Tags php and phpinfo subdomain

One. enable rewrite in Apache configuration

Open the configuration file httpd.conf:

1. Enable rewrite
# LoadModule Rewrite_module modules/mod_rewrite.so Remove the front #

2. Enable. htaccess
In the virtual machine configuration item
AllowOverride None modified to: allowoverride all

3. Open Phpinfo to see (example: http://localhost/?phpinfo=1)

Second, the basic wording of rewrite

The main function of Rewirte is to implement a URL jump, and its regular expression is based on the Perl language. Can be based on both server-level (httpd.conf) and directory-level (. htaccess) methods.

The server has a configuration file can not be changed by us, so in most cases, in the root directory of the site to build a. htaccess file.


Rewriteengine on//start rewrite engine
Rewriterule ^/index ([0-9]*). html$/index.php?id=$1//"([0-9]*)" represents the scope with (. *) for all, hereinafter.
Rewriterule ^/index ([0-9]*)/$/index.php?id=$1 [R]//virtual directory Note: rewriterule regular substitution ($1,$2 is the same as in the regular, representing the brackets) "but there is no escape here." Iii. Specific circumstances

1. Remove the WWW tag from the domain name

Rewritecond%{http_host}!^jb51\.net$ [NC]
Rewriterule.? Http://jb51.net%{request_uri} [R=301,l]
2. Remove the WWW tag, but save the subdomainRewritecond%{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 subdomainsRewritecond%{http_host} ^ ([A-Z.] +)? jb51\.net$ [NC]
Rewritecond%{http_host}!^www\. [NC]
Rewriterule.? Http://www.%1jb51.net%{request_uri} [R=301,l]
This rule crawls the%1 variable of level two domain name, if not start with www, then add www, the former 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. 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 uses [F]flag to block URLs ending with gif|jpg|png
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. 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 pictures: 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} can be.
5. Redirect to page 404 If file does not exist
If your host does not provide a 404 page redirection service, then we create it ourselves. 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 if your file name and path name exist before you make a 404 redirect. You can also add one on the 404 page? url=$1 parameter: Rewriterule ^/? (. *) $/404.php?url=$1 [L]
This way, your 404 page can do something else, 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: 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. : 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 suffix, try this: 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, and then replace it: 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: Rewritecond%{query_string}!uniquekey=
Rewriterule ^/?script_that_requires_uniquekey\.php$ other_script.php [qsa,l]
The above rule will check if the UniqueKey parameter exists in {query_string}, and if the {Request_uri} value is Script_that_requires_uniquekey, it will be directed to the new URL.
10. 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 save the original query variable with [Qsa,l]flag])
b). Add a "?" after the file name (e.g. index.php?). Symbol "?" Does not appear in the browser's address bar.
11. Present the current URI in a new format
If this is the urls:/index.php?id=nnnn we are currently running. 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 "marker" tag to the existing parameter by [Qsa]flag]. Here's how it's implemented: 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, does not contain marker, so the first rule is permanently redirected to http://www.jb51.net/nnnn, the second rule will be HTTP// WWW.JB51.NET/NNNN is redirected to http://www.jb51.net/index.php?marker&id=nnnn and adds marker and id=nnnn two variables, and finally Mod_ Rewrite begins the processing process.
Second match, marker is matched, so ignore the first rule, here is a "." The characters appear in 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.
12. 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: 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 Rewritecond%{request_uri} ^secure_page\.php$
Rewritecond%{server_port}!^443$
Rewriterule ^/? (secure_page\.php) $ https://www.jb51.net/$1 [r=301,l]
13. Enforce security services on a specific page
There is a security 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 following list of page requirements for security services: 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: 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.

. htaccess

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.