Htaccess rewrite pseudo-static syntax and common examples

Source: Internet
Author: User

Htaccess is a convenient pseudo-static rule for friends who do not have server permissions or have one server running N sites.

. Htaccess sets the default homepage order of a website

Add the following lines to the. htaccess file:

The Code is as follows: Copy code

DirectoryIndex index.html index.htm index. php

How to set the 404 error page in htaccess

Setting the 404 error page for Apache Server is simple. You only need to add the following code to the. htaccess file:

The Code is as follows: Copy code

ErrorDocument 404/notfound.html

Note:
1. Do not redirect the 404 error to the website homepage. Otherwise, the homepage may disappear from the seo/seo.html "target =" _ blank "> search engine.
2. Do not use an absolute URL (for example, http://www.hzhuti.com/nokia/n96/form). If you use an absolute URL, the returned status code is "302" + "200" (tested)

The correct method is to add the following code to. htaccess:

 

The Code is as follows: Copy code

1 ErrorDocument 404/404 .htm


Instead:


1 ErrorDocument 404 http://www.xxx.com/404.htm


The former returns a 404 status code,

The latter returns a 200 status code. If the content of a 404 page is smaller than 404 B, IE considers the 404 page unfriendly and will not return the error page in IE.


Add a backslash at the end of the URL in htaccess

Add the following code to the htaccess file:

The Code is as follows: Copy code

IfModule mod_rewrite.c
RewriteCond % {REQUEST_URI}/+ [^.] + $
RewriteRule ^ (. + [^/]) $ % {REQUEST_URI}/[R = 301, L]
/IfModule


301 redirect. htaccess will jump to the URL with www without www


In htaccess, replace pcjs.cn with your url:

The Code is as follows: Copy code

Options + FollowSymLinks
RewriteEngine on
RewriteCond % {http_host} ^ bKjia. c0m [NC]
RewriteRule ^ (. *) $ http://www.bKjia. c0m/$1 [R = 301, L]


Use. htaccess anti-leech

The premise is that the mod_rewrite module in Apache is activated. Since we want to eliminate the mp3 file leeching, we also set the image leeching. below is my website root directory. the configuration in the htaccess file is for the reference of the person who is suffering from illness:

Code in the. htaccess file:

The Code is as follows: Copy code

RewriteEngine on
RewriteCond % {HTTP_REFERER }! ^ Http: // (www .)? BKjia. c0m/. * $ [NC]
RewriteRule. (txt | doc | jpg | gif | mp3 | zip | rar | png) $ http://www.bKjia. c0m [R = 301, L]

Function: not only does the website block/prohibit unauthorized access to the website, but also transfers the request to its own website to let readers know who is the real author.

Code Description:
RewriteCond condition: It means all mp3, rar, images, etc. If the referer is not the website pcjs.cn or www.pcjs.cn,
RewriteRule rules: automatically go to the home page of this site

The commented out part is used to test the rewrite engine: It is useful for debugging mod_rewrite.

For other anti-leech settings, refer:

The Code is as follows: Copy code

# Enable the mod_rewrite Engine
RewriteEngine On
# Set valid request sources
RewriteCond % {HTTP_REFERER }! ^ $ [NC]
RewriteCond % {HTTP_REFERER }! Hzhuti.com [NC]
RewriteCond % {HTTP_REFERER }! Google.com [NC]
RewriteCond % {HTTP_REFERER }! Baidu.com [NC]
RewriteCond % {HTTP_REFERER }! Yahoo.com [NC]
RewriteCond % {HTTP_REFERER }! Msn.com [NC]
# Pointing to the warning page Image
RewriteRule. *. (gif | jpg | png | mp3) $ warning.png [L, NC]

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.