Apahce. htaccess redirection 301 302 and image anti-Leech rules

Source: Internet
Author: User
Tags ftp php file

Make the. htaccess file take effect

In windows2003, how does one configure apache to support. htaccess? The method is as follows:

1. In the Apache configuration file httpd. conf, find # LoadModule rewrite_module modules/mod_rewrite.so, and remove the "#" and save it.

2. Configure the virtual host file httpd-vhosts.conf, highlight the key color.

The code is as follows: Copy code

<Directory "website Directory">
Options-Indexes FollowSymLinks
AllowOverride All
</Directory>

3. Restart Apache and OK

 

How to create a. htaccess file

Directly creating a file named [. htaccess] cannot be implemented. This is a file with only an extension and no file name. The system will prompt you to enter a file name. We can achieve this using the following methods:
1. Download this file;
2. Use FTP, DW, notepad, and other software to create this file. FTP can directly create this file. If the software is used, create a new file and save it;
3. Use CMD or another operating system interface to rename the file to. htaccess ];
4. The [. htaccess] file is usually stored in the root directory or in a subdirectory. Its validity range is relative to the current path. The path rules are consistent with those in html.

Common formats

The following is a typical htaccess file.

# Enable URL rewriting RewriteEngine on
# URL rewriting scope RewriteBase/path/to/url
# What conditions are met: RewriteCond % {HTTP_HOST }! ^ Www.example.com $ [NC]
# What kind of rule RewriteRule is applied .? Http://www.example.com % {REQUEST_URI} [R = 301, L]
Let's take a look at RewriteCond. First, there is a %, because {HTTP_HOST} is an apache variable and needs to be indicated by %. From! The start is the matching condition. Regular expressions are supported .! Not equal to: if HTTP_HOST is not www.example.com. The following [NC] (no case) indicates that case sensitivity is ignored.

[L] (last): terminates a series of RewriteCond and RewriteRule
[R] (redirect): triggers a displayed jump. You can also specify the jump type, for example, [R = 301].
[F] (forbidden): disable viewing specific files. apache will trigger the 403 error.
Typical applications

Image anti-Leech

The code is as follows: Copy code
RewriteCond % {HTTP_REFERER }! ^ $
RewriteCond % {HTTP_REFERER }! ^ Http: // (www .)? Example.com/[NC]
RewriteRule. (gif | jpg | png) $-[F]

Because it is based on HTTP_REFERER verification, it can only prevent normal image leeching, because HTTP_REFERER is more prone to forgery.

Custom Error 404 page
If you enter a url that does not exist, the custom error page is displayed.

The code is as follows: Copy code
ErrorDocument 404/404 .html # others likewise ErrorDocument 500/500 .html

Process moved files

The code is as follows: Copy code

Redirect 301/old.html/new.html

# RewriteRule/old.html/new.html [R = 301, L]

# If you want to perform implicit jump (the URL address remains unchanged but the content is actually from other URLs), use the following RewriteRule/old.html/new.html [L]
There are many articles for RewriteRule, such

 
# Link an html suffix url to a php file
#$1 refers to the RewriteRule ^ /? ([A-z/]+).html $ 1.php [L]
# Or link the content of the old folder to the new folder RewriteRule ^ /? Old_directory/([a-z/.] +) $ new_directory/$1 [R = 301, L]

# Hide the file name RewriteRule ^ /? ([A-z] +) $ 1.php [L]
Disable Directory List Display
If there is no index file in the directory and no special processing is performed on the Directory, especially on windows hosts, the contents in the directory will be displayed, in this case, you can create. htaccess file, and then write

Options-Indexes # that's all you need.
Block/allow specific IP addresses/IP segments

The code is as follows: Copy code
# Disable all IP addresses except the specified order deny and allow deny from all
# If you want to allow IP segments, such as 123.123.123.0 ~ 123.123.123.255, then
# Allow from 123.123.123. allow from 123.123.123.123 ErrorDocument 403/page.html <Files page.html> allow from all </Files>

# If you want to disable a specific IP address deny from 123.123.123.123
Add MIME type

The code is as follows: Copy code
AddType video/x-flv. flv

# If the setting type is application/octet-stream, the system will prompt you to download AddType application/octet-stream. pd.

Pseudo static path

Pseudo static path: [http://www.abc.com/news/post-16.html]
Real path: [http://www.abc.com/index.php? M = post & c = index & catdir = news & id = 16]
[. Htaccess] statement:

The code is as follows: Copy code
================
RewriteEngine on
RewriteRule http://www.abc.com/([a-zA-Z] {1,})/post-(%0-9%%1,%%%.html $ http://www.abc.com/index.php? M = post & c = index & catdir = $1 & id = $2
================
Of course, if it is placed in the root directory, use the relative path:
================
RewriteEngine on
RewriteRule ([a-zA-Z] {1,})/post-(%0-9%}1 %}.html index. php? M = post & c = index & catdir = $1 & id = $2
================

 
Explanation:
Note: Due to display problems, spaces may not be visible. You can copy the code and paste it to other software.
In the file, "RewriteEngine on" indicates that rewrite is enabled;
RewriteRule indicates a rule. Each row contains one rule. The format is RewriteRule, which matches the real URL of the script submitted by the user ];
The dyeing part in the path is a parameter, and the regular expression is used for matching, the parameters in the real path use [$1], [$2], and [$3] to replace the regular expression in the URL from left to right.

Verification:

The code is as follows: Copy code

Create a. htaccess file and write the following content:
================
RewriteEngine on
RewriteRule ([a-zA-Z] {1,})/post-(%0-9%}1 %}.html test. php? Get1 = $1 & get2 = $2
================
Save.
 
Create a test. Php file and write the following content:
================
<? Php
Echo 'Your get1 is: '. $ _ GET ['get1'];
Echo '<br/> ';
Echo 'Your get2 is: '. $ _ GET ['get2'];
?>
================
Save to the same directory.
 
Access/news/post-15.html
If the page displays:
================
Your get1 is: news
Your get2 is: 15
================
It is declared successful.

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.