Linux. htaccess set cache and gzip compression configuration

Source: Internet
Author: User

In the use of Linux host, the general use of the environment are apache+php+mysql to use, in fact, with your use of PHP, Mysql does not matter.

The key is Apache settings, generally as long as the httpd.conf set up. Htaccess can be used normally. about how to set up can Baidu ~ This is not the focus of this article.

How to use. htaccess to set browser caching

Generally speaking our website picture file (jpg,png,gif etc.), the style file (CSS), the script file (JS) These are not frequently changes, then we also do not need to let the user open the browser to re-read these files each time. There are several disadvantages to not setting the cache

Waste of bandwidth traffic

Bad for the user experience
What's more important is the friendliness of the search engine to your site. The website responds quickly, often can obtain the search engine's affection more.
You can complete the browser's cache by adding the following settings to the. htaccess

  code is as follows copy code

<if Module mod_expires.c>
expiresactive on
expiresdefault A600
Expiresbytype image/x-icon A2592000
Expiresbytype application/x-javascript A2592000
expiresbytype text/css A604800
Expiresbytype image/gif A2592000
Expiresbytype image/png A2592000
expiresbytype image/jpeg A2592000
Expiresbytype text/plain A86400
Expiresbytype application/x-shockwave-flash A2592000
expiresbytype video/x-flv A2592000
Expiresbytype Application/pdf A2592000
Expiresbytype text/html A600
</ifmodule>

Cache time, where setting A2592000 represents the cache time in the browser, 2.592 million seconds = 30 days.


how to use. htaccess to turn on gzip compression

We need to be in the user access time compressed file stream for transmission, so that the small file stream to the user's browser, thus improving the browsing response speed and reduce the bandwidth flow, we use the following settings, in. htaccess add the following settings to complete gzip compression.

The code is as follows Copy Code

<ifmodule mod_deflate.c>
Addoutputfilter DEFLATE HTML xml PHP js CSS
</ifmodule>

Use. htaccess to turn off Fileetag and optimize response header files

If you are not familiar with the response header file, and do not use the head file validation and other methods, then you can directly turn off the settings to improve the speed of the response. In the. htaccess, set the following settings to close the Fileetag.

Fileetag None

After these optimizations, presumably the website speed will be much faster. More settings, you can look at other htaccess related articles, or in this article to answer questions.

Extension:. htaccess redirection and URL rewriting

A simple redirect

Redirect ^old.html$ http://localhost/new.html

This is to set an HTTP status code of 301 (permanently moved) and turn all Access old.html to new.html. A regular expression is used to match the visited page, and the following URL must be the full path.

URL Rewrite

Rewriteengine on
Rewriterule ^old.html$ new.html

The first line opens the override engine

The second line is the overriding rule. This allows the user to access the old.html is actually a visit to the new.html, and users of this well informed. Because the address bar still shows old.html.

In order to display the new access address in the address bar, we can add an F sign in the Rewriterule statement.

Rewriterule ^old.html$ http://hostname/new.html [r=301]

Here is the URL with the query parameter rewritten to the search engine friendly URL

Rewriterule ^products/([^/]+)/([^/]+)/([^/]+) product.php?cat=$1&brand=$2&prod=$3

This rule allows visitors to do something like this
URL products/turntables/technics/sl1210,
To access product.php?cat=turntables&brand=technics&prod=sl1210.

A unit in parentheses corresponding to the $1,$2,$3 of the rear. [^/]+ any one or more characters except/in the original brackets.

Custom error page

The code is as follows Copy Code

ErrorDocument 404 "/404.html"

This is the definition of an error that 404 pages cannot find, or a page that can define other error codes.

Restricting access to feature resources

The code is as follows Copy Code

<files "Protectedfile.html" >
AuthName "Username and password required"
authuserfile/path/to/.htpasswd
Require Valid-user
AuthType Basic
</Files>

Protection Protectedfile.html,authname is the message of the landing box, AuthUserFile is the path to the. htpasswd file

Block certain IP access

The code is as follows Copy Code

Order Allow,deny
Deny from 192.168.0.1
Allow from Allorder

Tells the order of Apache directives. This one here is first allowed and then rejected. Although allow from all appears, it still takes effect before. Denied 192.168.0.1 's visit. You can also block access by paragraph, as long as you specify only the preceding paragraphs to be similar, 192.168.

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.