Use the. htAccess file to set the 301 redirection/404 error page/pseudo-static method

Source: Internet
Author: User
Tags website server
After purchasing a server space, a friend who built the website for the first time may call the root directory of the space. the htaccess file is confused. I don't know what it is or what it is used for. This article will explain in detail what is under the root directory of the website server. what is an htaccess file. The. htaccess file is Apache.

After purchasing a server space, a friend who built the website for the first time may call the root directory of the space. the htaccess file is confused. I don't know what it is or what it is used for. This article will explain in detail what is under the root directory of the website server. what is an htaccess file.

The. htaccess file is a configuration file (not available on the Nginx server) on the Apache server. it is responsible for the webpage configuration under the relevant directory. Through. setting the htaccess file can help us achieve: webpage 301 redirection, custom 400/403/404/500 error page, change file extension, allow/block access by specified IP users, disable directory list, configure default documents, and other functions are very powerful, the following describes how to set the most common functions.

Set website error page

ErrorDocument 400/error_pages/400.html
ErrorDocument 401/error_pages/401.html
ErrorDocument 403/error_pages/403.html
ErrorDocument 404/error_pages/404.html
ErrorDocument 500/error_pages/500.html

Set webpage 301 redirection

# REDIRECT from the old_dir directory to the new_dir Directory
Redirect/old_dir/http://www.yourdomain.com/new_dir/index.html
# Redirect requests accessed through the second-level directory to the second-level domain name
RedirectMatch 301/dir/(. *) http://dir.yourdomain.com/#1

Prohibit access from users of specified IP segments

# Prohibit access from IP address ranges 0.0.0 and 123.45.6.
Order allow, deny
Deny from 255.0.0.0
Deny from 123.45.6.
Allow from all

Prohibit access to the specified source webpage

# Prohibit access from sources of otherdomain.com and anotherdomain.com
RewriteEngine on
# Options + FollowSymlinks
RewriteCond % {HTTP_REFERER} otherdomain \. com [NC, OR]
RewriteCond % {HTTP_REFERER} anotherdomain \. com
RewriteRule .*? [F]

Anti-Leech settings

# All images accessed from domain names other than this site are displayed as feed.jpg
RewriteEngine on
RewriteCond % {HTTP_REFERER }! ^ $
RewriteCond % {HTTP_REFERER }! ^ Http: // (www \.)? Yourdomain.com/.?##nc]
RewriteRule \. (gif | jpg | png) $ http://www.yourdomain.com/feed.jpg?r,l]

Set the home page of a folder

When the hosts file is folder, the server will search for index.html and use it as the homepage file. if not, search for it in turn.
DirectoryIndex index.html index. cgi index. php

Set multimedia files to download, not play

AddType application/octet-stream. mp3. mp4

Custom HTTP header

Header set X-Pingback "http://www.yourdomain.com/xmlrpc.php"
Header set article-by "yourdomain.com"

Set the file expiration time Cache Control

# Enable validity period control
ExpiresActive On
# Gif/png/jpg is valid for 1 month
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType image/jpg "access plus 1 month"
# Js/css is valid for one week
ExpiresByType text/javascript "access plus 1 week"
ExpiresByType text/css "access plus 1 week"

Pseudo-static code of WordPress website builder

# BEGIN WordPress # This is a line of comment, indicating that WordPress's htaccess starts from here
# If Apache loads the mod_rewrite.c module, run the following code:
RewriteEngine On # enable mod_rewrite engine
RewriteBase/# set the baseline URL for directory rewriting/
RewriteRule ^ index \. php $? [L] # if the request path is index. php, stop the rewrite operation (to avoid endless loops)
RewriteCond % {REQUEST_FILENAME }! -F # if the request is not a file, continue processing
RewriteCond % {REQUEST_FILENAME }! -D # if the requested directory is not one, continue processing
RewriteRule./index. php [L] # direct all requests to/index. php
# End IfModule
# END WordPress # WordPress's htaccess ends here

Discuz x3/x3.1 common pseudo-static code

# If Apache loads the mod_rewrite.c module, run the following code:
RewriteEngine On
RewriteBase/Discuz
RewriteCond % {QUERY_STRING} ^ (. *) $
RewriteRule ^ topic-(. +) \. html $ portal. php? Mod = topic & topic = $1 & % 1
RewriteCond % {QUERY_STRING} ^ (. *) $
RewriteRule ^ article-([0-9] +)-([0-9] +) \. html $ portal. php? Mod = view & aid = $1 & page = $2 & % 1
RewriteCond % {QUERY_STRING} ^ (. *) $
RewriteRule ^ forum-(\ w +)-([0-9] +) \. html $ forum. php? Mod = forumdisplay & fid = $1 & page = $2 & % 1
RewriteCond % {QUERY_STRING} ^ (. *) $
RewriteRule ^ thread-([0-9] +)-([0-9] +)-([0-9] +) \. html $ forum. php? Mod = viewthread & tid = $1 & extra = page \ % 3D $3 & page = $2 & % 1
RewriteCond % {QUERY_STRING} ^ (. *) $
RewriteRule ^ group-([0-9] +)-([0-9] +) \. html $ forum. php? Mod = group & fid = $1 & page = $2 & % 1
RewriteCond % {QUERY_STRING} ^ (. *) $
RewriteRule ^ space-(username | uid)-(. +) \. html $ home. php? Mod = space & $1 = $2 & % 1
RewriteCond % {QUERY_STRING} ^ (. *) $
RewriteRule ^ blog-([0-9] +)-([0-9] +) \. html $ home. php? Mod = space & uid = $1 & do = blog & id = $2 & % 1
RewriteCond % {QUERY_STRING} ^ (. *) $
RewriteRule ^ archiver/(fid | tid)-([0-9] +) \. html $ archiver/index. php? Action = $1 & value = $2 & % 1
RewriteCond % {QUERY_STRING} ^ (. *) $
RewriteRule ^ ([a-z] + [a-z0-9 _] *)-([a-z0-9 _ \-] +) \. html $ plugin. php? Id = $1: $2 & % 1
# End IfModule

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.