A sample collection of practical configurations for the. htaccess file in the Apache server _linux

Source: Internet
Author: User
Tags parent directory wordpress blog

. What's htaccess?

Htaccess (hypertext access) is a file that provides site owners with the option to control server environment variables and other parameters, thereby enhancing the functionality of their web site. These files can be in any directory in the Web site Directory tree and provide functionality for the directory and the files and subdirectories in the directory.

What are these features? These are the instructions of the server, such as the rows where the command server performs specific tasks, which are valid only for files and subdirectories in the directory in which the file resides. These files are hidden by default because all operating systems and Web servers are configured to ignore them by default, but you can see these special files if you view hidden files. The topic of subsequent chapters will discuss what types of parameters can be controlled.

Note: If the. htaccess file is saved in the/apache/home/www/gunjit/directory, it will supply commands to all files and subdirectories in the directory, but if the directory contains a subdirectory named/gunjit/images/, and there is also a . htaccess file, the commands in this subdirectory overwrite the commands provided by the. htaccess file in the parent directory (or a file that is higher in the directory hierarchy).

Apache Server and. htaccess files

The Apache HTTP Server, commonly known as Apache, is named for the respect of a Native American tribe with a superior war strategy skill. Based on NCSA HTTPd server, it is a cross-platform Web server built with C/s + + and XML, which plays a key role in the growth and development of the World Wide Web.

It is most commonly used in UNIX, but Apache can also be used on a variety of platforms, including FreeBSD, Linux, Windows, Mac OS, novel Netware, and so on. In 2009, Apache became the first server to provide services to more than 100 million sites.

The Apache server can have a separate. htaccess file for each user in the www/directory. Although these files are hidden, they can be made visible if needed. There can be many subdirectories in the www/directory, each named with a user name or owner name, including a site. In addition, you can have a. htaccess file in each subdirectory, as described previously for configuring files in subdirectories.

The following describes if you configure the htaccess file on the Apache server.

Configuration on the Apache server

Here are two things:

Hosting a Web site on your own server

In this case, if the. htaccess file is not enabled, you can find a partial activation in the http.conf (the default configuration file for the Apache http daemon).

<directory "/var/www/htdocs" >

Position as Downlink

Allowoverridenone

Change to

Allowoverrideall

Now, when you restart Apache, you are enabled. htaccess.

11 practical Apache. htaccess configurations
1. Force suffix back slash
at the end of the URL plus a backslash seems to be beneficial to SEO:)

<ifmodule mod_rewrite.c> 
 rewritecond%{request_uri}/+[^\.] +$ 
 rewriterule ^ (. +[^/]) $%{request_uri}/[r=301,l] 
</IfModule> 

2. Anti-Theft Chain
Save your precious bandwidth!

Rewriteengine on 
#Replace? Mysite\.com/with your blog URL 
rewritecond%{http_referer}!^http://(. +\.)? mysite\.com/[NC] 
rewritecond%{http_referer}!^$ 
#Replace/images/nohotlink.jpg with your "don ' t Hotlink" Image URL 
rewriterule. *\. ( Jpe?g|gif|bmp|png) $/images/nohotlink.jpg [L] 

3. REDIRECT Mobile devices
Join your website to support mobile device access, it is best to redirect mobile device access to a specially customized page

Rewriteengine on Rewritecond%{request_uri}!^/m/.*$ rewritecond%{http_accept} "Text/vnd.wap.wml|application/vnd.wap . Xhtml+xml "[Nc,or] Rewritecond%{http_user_agent}" acs|alav|alca|amoi|audi|aste|avan|benq|bird|blac|blaz|brew| Cell|cldc|cmd-"[Nc,or] Rewritecond%{http_user_agent}" dang|doco|eric|hipt|inno|ipaq|java|jigs|kddi|keji|leno| Lg-c|lg-d|lg-g|lge-"[Nc,or] Rewritecond%{http_user_agent}" maui|maxo|midp|mits|mmef|mobi|mot-|moto|mwbp|nec-| NEWT|NOKI|OPWV "[Nc,or] Rewritecond%{http_user_agent}" palm|pana|pant|pdxg|phil|play|pluc|port|prox|qtek|qwap| Sage|sams|sany "[Nc,or] Rewritecond%{http_user_agent}" sch-|sec-|send|seri|sgh-|shar|sie-|siem|smal|smar|sony| Sph-|symb|t-mo "[Nc,or] Rewritecond%{http_user_agent}" teli|tim-|tosh|tsm-|upg1|upsi|vk-v|voda|w3cs|wap-|wapa| Wapi "[Nc,or] Rewritecond%{http_user_agent}" wapp|wapr|webc|winw|winw|xda|xda-"[Nc,or] Rewritecond%{HTTP_USER_ AGENT} "UP.BROWSER|UP.LINK|WINDOWSSCE|IEMOBILE|MINI|MMP" [Nc,or] Rewritecond%{http_user_ageNT} "SYMBIAN|MIDP|WAP|PHONE|POCKET|MOBILE|PDA|PSP" [NC] #-------------The line below excludes the IPad Rewritecond%{ht Tp_user_agent}!^.*ipad.*$ #-------------Rewritecond%{http_user_agent}!macintosh [NC] #*see Note BELOW rewriterule ^ 
 (. *) $/m/[l,r=302]

4. Force the browser to download the specified file type
you can force browsers to download certain types of files, rather than reading and opening them, such as MP3, XLS.

<files *.xls> 
 forcetype application/octet-stream 
 Header set content-disposition attachment 
</ files> 
<files *.eps> 
 forcetype application/octet-stream 
 Header set content-disposition Attachment 
</Files> 

5. Firefox's cross domain font embedding
Firefox does not allow embedding an outer-station font, the following. htaccess fragment can bypass this restriction

<filesmatch "\. (Ttf|otf|eot|woff) $ "> 
<ifmodule mod_headers.c> 
  Header set Access-control-allow-origin" http:// YourDomain.com " 
</IfModule> 
</FilesMatch> 

6. Use. htaccess cache to speed up the website
I'm afraid this is the most useful piece of code. This code can help you greatly improve the speed of the website!

# 1 year 
<filesmatch "\. ( ico|pdf|flv) $ "> 
Header Set Cache-control" max-age=29030400, public " 
</FilesMatch> 
# 1 WEEK 
<filesmatch "\. (jpg|jpeg|png|gif|swf) $ "> 
Header Set Cache-control" max-age=604800, public " 
</FilesMatch> 
# 2 Days 
<filesmatch "\. ( XML|TXT|CSS|JS) $ "> 
Header Set Cache-control" max-age=172800, proxy-revalidate " 
</FilesMatch> 
# 1 MIN 
<filesmatch "\. ( html|htm|php) $ "> 
Header Set Cache-control" max-age=60, Private, proxy-revalidate " 
</FilesMatch> 

7. Prevent WordPress blog spam comments
still having a headache for spam comments? You can use the Akismet plugin to solve this problem, but. htaccess files come more directly: stop spam comment bots from accessing wp-comments-post.php files

<ifmodule mod_rewrite.c> 
rewriteengine on 
rewritecond%{request_method} POST 
Rewritecond%{ Request_uri}. wp-comments-post\.php* 
rewritecond%{http_referer}!. *yourdomainname.* [OR] 
rewritecond%{http_user_agent} ^$ 
rewriterule (. *) ^http://%{remote_addr}/$ [R=301,L] 
</IfModule> 

8. redirect different feed formats to a unified format
many years ago, there were many different feed formats, such as RSS, Atom, RDF, and so on. But now RSS has taken the absolute lead. The following code allows you to redirect different feed formats to the same feed. This code can be used directly on the WordPress blog.

<ifmodule mod_alias.c> 
 redirectmatch 301/feed/(atom|rdf|rss|rss2)/?$ 
 http://example.com/feed/ Redirectmatch 301/comments/feed/(atom|rdf|rss|rss2)/?$ http://example.com/comments/feed/ 
</IfModule> 

9. Configure the site's HTML5 video
HTML5 brings us no flash video playback, but you must configure your server to provide the latest HTML5 video playback capabilities.

Rewritecond%{request_filename}!-f 
rewritecond%{request_filename}!-d rewritecond%{REQUEST_URI 
}!=/ Favicon.ico 
addtype video/ogg. OGV 
addtype video/ogg. 
ogg AddType video/mp4. mp4 AddType. 
WEBM 
AddType Application/x-shockwave-flash swf 

10. Log PHP Errors
It is awkward and unsafe to display PHP errors on the page, and the following code can log PHP errors to the. log file instead of the page display.

# Display no errs to user 
Php_flag display_startup_errors out 
php_flag display_errors off 
php_flag html_ Errors off 
# log to file 
php_flag log_errors on 
php_value error_log/location/to/php_error.log 

11. Run PHP in JavaScript code
inserting PHP code in JS is sometimes useful, such as reading a database. The following code allows you to run PHP in JS.

AddType application/x-httpd-php. js 
AddHandler x-httpd-php5. js 
 
<filesmatch "\. ( js|php) $ "> 
sethandler application/x-httpd-php 
</FilesMatch> 

12.404 page jump, jump to 404.php, according to the URL record does not exist the path of the page

<ifmodule mod_rewrite.c> 
rewriteengine on 
 
rewritecond%{request_filename}!-f  
Rewritecond%{ Request_filename}!-d  
rewritecond%{query_string}!^$ rewriterule 
? (. *) $/404.php?url=%{http_host}%{request_uri}?%{query_string} [NC]  
 
rewritecond%{request_filename}!-f  
Rewritecond%{request_filename}!-d  
rewritecond%{query_string} ^$ rewriterule 
? (. *) $/404.php?url=%{http_host}%{request_uri} [NC]  
 
</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.