13 practical Apache Rewrite rewrite rules

  • 2020-05-09 19:45:14
  • OfStack

1. Remove the www tag from the domain name

RewriteCond %{HTTP_HOST} !^jb51\.net$ [NC]
RewriteRule .? http://ofstack.com%{REQUEST_URI} [R=301,L]

2. Remove the www tag, but save the subdomain
RewriteCond %{HTTP_HOST} ^www\.(([a-z0-9_]+\.)?jb51\.net)$ [NC]
RewriteRule .? http://%1%{REQUEST_URI} [R=301,L]

In this case, the subdomain is only fetched in %2 (the inner atom) when the 1% variable is matched, which is exactly the %1 variable we need.
3. Tag the subdomain with www
RewriteCond %{HTTP_HOST} ^([a-z.]+)?jb51\.net$ [NC]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .? http://www.%1ofstack.com%{REQUEST_URI} [R=301,L]

This rule grabs the %1 variable for level 2 domain names. If it does not start with www, then add www. The previous domain name and {REQUEST_URI} will follow.
4. Prevent images from hotlinking
Some stationmaster unscrupulous will your picture hotlinking on their website, consume your bandwidth. You can add 1 code to prevent 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 prevents URL ending in gif|jpg|png with [F]FLAG
If you absolutely despise this type of hotlinking, you can also change the image to let visitors to hotlinking know that the site is misusing your image.
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?jb51\.net/.*$ [NC]
RewriteRule \.(gif|jpg|png)$  Your picture address  [R=301,L]

In addition to preventing images from being linked to hotlinking, the above rules replace all hotlinking images with the ones you set.
You can also block specific domain name hotlinking from your images:
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, the above rules are based on {HTTP_REFERER} to get the domain name, if you want to use the IP address, use {REMOTE_ADDR}.
5. If the file does not exist, redirect to the 404 page
If your host does not provide a 404 page redirect 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. This code will determine if your file name and pathname exist before doing a 404 redirect. You can also add one to a 404 page, right? url = $1 parameters:
RewriteRule ^/?(.*)$ /404.php?url=$1 [L]

This way, your 404 page can do other things, such as default confidence, send 1 email reminder, add 1 search, and so on.
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 of it) to match the suffix name of the file.
7. Convert the.html suffix to.php
If the.html file can continue to be accessed, update the link to your website.
RewriteRule ^/?([a-z/]+)\.html$ $1.php [L]

This is not a page redirect, so visitors are not visible. Have him modify FLAG as a permanent redirect (visible) [R=301,L].
8. Create a fileless suffix name link
If you want to make the links to your PHP site easier to remember - or hide the file's suffix name - try this:
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 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 the.php suffix, this rule will be enforced.
9. Check for specific parameters in the query variable
If there is a special parameter in URL, you can verify its existence with RewriteCond:
RewriteCond %{QUERY_STRING} !uniquekey=
RewriteRule ^/?script_that_requires_uniquekey\.php$ other_script.php [QSA,L]

The above rule will check if 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 Apache mod_rewrite module will automatically identify the 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 "? "to the end of 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
What if this is the URLs: / index.php we are currently running? id = nnnn. We would very much like to change it to /nnnn and have the search engine present it in a new format. First, we had to redirect the old URLs to the new format in order to update the search engine, but we also had to make sure that the old index.php still worked. Am I confusing you?
The trick is to add an invisible "marker" marker to 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 an "marker" tag to the existing parameter by [QSA]FLAG. Here's how:
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: / / www ofstack. com/index php? id = nnnn, does not contain marker, so was the first rule of permanent redirect to / / www ofstack. com/nnnn, second rule will / / www ofstack. com/nnnn the redirect to / / www ofstack. com/index php? marker&id=nnnn, marker and id=nnnn are added. Finally, mod_rewrite starts the process.
Second match, marker be matched, so ignore the first rule, there is a ". "character will appear in / / www ofstack. com/index php? marker&id=nnnn, so rule 2 is also ignored, and we're done.
Note that this solution requires some extension of Apache, so there are a lot of obstacles if your site is placed on a Shared host.
12. Ensure that security services are enabled
Apache can tell if you have turned on a security service in two ways, using the {HTTPS} and {SERVER_PORT} variables:
RewriteCond %{REQUEST_URI} ^secure_page\.php$
RewriteCond %{HTTPS} !on
RewriteRule ^/?(secure_page\.php)$ https://www.ofstack.com/$1 [R=301,L]

The above rule tests whether the {REQUEST_URI} value is equal to our safe 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 do the same test with {SERVER_PORT}, 443 being the common security service port
RewriteCond %{REQUEST_URI} ^secure_page\.php$
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^/?(secure_page\.php)$ https://www.ofstack.com/$1 [R=301,L]

13. Enforce a security service on a specific page
When you encounter 1 security service domain and 1 non-security service domain under the same server root directory, you need to use RewriteCond to determine whether the security service port is occupied, and only the following list of pages are required for security service:
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^/?(page1|page2|page3|page4|page5)$ https://www.ofstack.com/%1[R=301,L]

Here's 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)$//www.ofstack.com%{REQUEST_URI} [R=301,L]

In fact, regular expressions should be used most in Rewrite. If you know something about regular expressions, it is relatively easy to write this rule.


Related articles: