Home >Backend Development >PHP Tutorial >Explain the relevant knowledge of RewriteCond and 13 mod_rewrite application examples Apache pseudo-static

Explain the relevant knowledge of RewriteCond and 13 mod_rewrite application examples Apache pseudo-static

jacklove
jackloveOriginal
2018-06-11 10:58:312281browse

1. Add the www tag to the subdomain name
RewriteCond %{HTTP_HOST} ^([a-z.] )?example\.com$ [NC]
RewriteCond %{HTTP_HOST} !^www\. [ NC]
RewriteRule .? http://www.xample.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 Just add www, followed by the previous domain name and {REQUEST_URI}.
2. Remove the www tag in 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 subdomain
RewriteCond %{HTTP_HOST} ^www\.(([a-z0-9_] \.)?example\.com) $ [NC]
RewriteRule .? http://%1%{REQUEST_URI} [R=301,L]
Here, when the 1% variable is matched, the subdomain name will be in %2 (internal atom ), and it is this %1 variable that we need.
4. Prevent hotlinking of pictures
#Some webmasters will do whatever it takes to hotlink your pictures to their websites, consuming your bandwidth. You can add code to prevent this behavior.
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?example\.com/ [NC]
RewriteRule \.(gif|jpg| png)$ - [F]
If the {HTTP_REFERER} value is not empty, or does not come from your own domain name, this rule uses [F]FLAG to block URLs ending with gif|jpg|png
If this You absolutely despise this type of hotlinking, and you can also change the picture to let users who visit the hotlinking website know that the website is stealing your pictures.
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?example\.com/.*$ [NC]
RewriteRule \.(gif |jpg|png)$ Your picture address [R=301,L]
In addition to preventing hotlinking of pictures, the above rules will replace all hotlinked pictures with the pictures you set.
You can also prevent specific domain names from hotlinking 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 {HTTP_REFERER} to obtain the domain name. If you want to change it to an IP address, just use {REMOTE_ADDR}.
5. If the file does not exist, redirect to the 404 page
If your host does not provide a 404 page redirection service, then we will create it ourselves.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .? /404.php [L]
Here -f matches the existing file name, - d matches the existing pathname. This code will determine whether your file name and path name exist before performing a 404 redirect. You can also add a ?url=$1 parameter to the 404 page:
RewriteRule ^/?(.*)$ /404.php?url=$1 [L]
In this way, your 404 page can be Some other things, such as default confidence, send an email reminder, add a search, etc.
6. Rename directory
If you want to rename a directory on your 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, there is an escape character in front) to match the suffix name of the file.
7. Convert the .html suffix to .php
Provided that the .html file can continue to be accessed, update your website link.
RewriteRule ^/?([a-z/] )\.html$ $1.php [L]
This is not a web page redirect, so it is not visible to visitors. Make it a permanent redirect (visible) and modify FLAG to [R=301,L].
8. Create links without file extensions
If you want to make links to your PHP website more concise and easy to remember - or hide file extensions, try this:
RewriteRule ^/?([ a-z] )$ $1.php [L]
If the website contains a mixture of 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 %{REQUEST_FILENAME}.html -f
RewriteRule ^/?([a-zA -Z0-9] )$ $1.html [L]
If the file has a .php suffix, this rule will be executed.
9. Check specific parameters in query variables
If there is a special parameter in the URL, you can use RewriteCond to identify its existence:
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 value of {REQUEST_URI} is script_that_requires_uniquekey, it will be directed to a new URL.
10. Delete query variables
Apache’s mod_rewrite module will automatically recognize query variables unless you make the following changes:
a). Assign a new query parameter (you can use [QSA,L] FLAG saves the original query variable)
b). Add a "?" after the file name (such as index.php?). The symbol "?" will not be displayed in the browser's address bar.
11. Display the current URI in the new format
if this is the URLs we are currently running: /index.php?id=nnnn. We'd love to change it to /nnnn and let search engines display it in the new format. First, in order to update the search engine to the new one, we have to redirect the old URLs to the new format, but we also have to ensure that the previous index.php can still run. Are you confused by me?
The trick to achieve the above function is to add a marker "marker" that is invisible to visitors in the query variable. We only redirect links that do not have the "marker" tag in the query variable, then replace the original link with the new format, and add a "marker" tag to the existing parameters through [QSA] FLAG. The following is the 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 does not contain marker, so it is permanently redirected to http://www.example.com/nnnn by the first rule, and http://www.example.com/nnnn by the second rule. //www.example.com/nnnn is redirected to http://www.example.com/index.php?marker&id=nnnn, and two variables, marker and id=nnnn, are added. Finally, mod_rewrite starts the processing process. .
In the second match, marker is matched, so the first rule is ignored. There is a "." character here that will appear in http://www.example.com/index.php?marker &id=nnnn, so The second rule will also be ignored, so we're done.
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 use two methods to identify whether you have enabled the security service, referencing 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 rules test whether the {REQUEST_URI} value is equal to our secure page code, and {HTTPS} is not equal to on. If these two conditions are met at the same time, the request will be redirected to the secure service URI. In addition, you can use {SERVER_PORT} to do the same test. 443 is a commonly used 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 specific pages
When you encounter a security service domain name and a non-security service domain name in the same server root directory, you need to use RewriteCond to determine whether the security service port is Occupy, and only require the pages in the following list to be secure services:
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^/?(page1|page2|page3|page4|page5)$ https://www .example.com/%1[R=301,L]
The following is how to return a page that is not set as a secure service to port 80:
RewriteCond %{ SERVER_PORT } ^443$
RewriteRule !^ /?(page6|page7|page8|page9)$http://www.example.com%{REQUEST_URI} [R=301,L]

This article explains RewriteCond and 13 A mod_rewrite application example and related knowledge of Apache pseudo-static. For more related content, please pay attention to the PHP Chinese website.

Related recommendations:

Understand the difference between forward proxy and reverse proxy

How to use Apache to build a virtual host

How to start and monitor the sh memcached process

The above is the detailed content of Explain the relevant knowledge of RewriteCond and 13 mod_rewrite application examples Apache pseudo-static. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn