Apache rewrite: an example of php pseudo-static html page

Source: Internet
Author: User
Tags html page

The apache rewrite rule has two common tags: [L] and [QSA]. After obtaining pseudo-static data with $ _ GET? The following parameter values must be marked with [QSA.

The differences between [L] and [QSA] are as follows:

1) 'Last | L' (last rule last) immediately stops the rewrite operation and does not apply other rewrite rules.
 
2) 'qsappend | qsa' (append request string append)

This flag forces the rewrite engine to append a request string to an existing replacement string, instead of simply replacing it. If you need to add information to the request string by using the rewrite rule, you can use this flag.
 
Example
 

The code is as follows: Copy code
For pseudo-static URLs/main/test? Page = 1
 
RewriteRule ^ main/([a-zA-Z0-9] +) $/index. php? C = main & m = index & a = $1 [L]
 
$ _ GET ['Page'] cannot be obtained
 
RewriteRule ^ main/([a-zA-Z0-9] +) $/index. php? C = main & m = index & a = $1 [QSA]
 
$ _ GET ['Page'] can be obtained

Example

The code is as follows: Copy code

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule index.html $ index. php
RewriteRule index-([1-9] + [0-9] * 2.16.html $ index. php? P = $1
RewriteRule ({a-z}{1, {}-(%0-9}}1 }}.html $ index. php? Action = $1 & id = $2
</IfModule>

The project in the example is test.

Write content to the index. php page of the project

The code is as follows: Copy code

<? Php
If ($ _ GET ['P']) {
Echo "p:". $ _ GET ['P'];
}

If ($ _ GET ['action']) {
Echo "action:". $ _ GET ['action'];
}

If ($ _ GET ['id']) {
Echo "id:". $ _ GET ['id'];
}
?>

Enter

Http: // localhost/test/index.html
Http: // localhost/test/index-99.html
Http: // localhost/test/page-18.html

All will go to the http: // localhost/test/index. php page

And
Http: // localhost/test/index.html the page is not displayed.
Http: // localhost/test/index-99.html page displays p: 99
Http: // localhost/test/page-18.html page displays action: pageid: 18

In addition to simple configuration, pseudo-static anti-leech can also be implemented. We only need to configure it briefly.

If you do not want other websites to call images on this website, you can add the following content to the. htaccess or apche configuration file httpd. conf.

The code is as follows: Copy code
RewriteEngine on
# Enable Rewrite module
RewriteCond % {HTTP_REFERER }! ^ $
# If you do not directly enter the image address
RewriteCond % {HTTP_REFERER }! Img. Your domain name $ [NC]
RewriteCond % {HTTP_REFERER }! Google.com [NC]
RewriteRule (. *)\. (jpg | jpeg | jpe | gif | bmp | png | wma | mp3 | wav | avi | mp4 | flv | swf) $ http: // your domain/err.jpg [R = 301, L, NC]

Screenshot capture all .jpg or .jpeg ...... Request, jump to http: // your domain name/err.jpg with an error message. Note: the image cannot be in the original domain name or in a folder effectively controlled by the. htaccess file.

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.