21 common apache.htaccess file configuration tips to share

  • 2020-05-09 19:46:10
  • OfStack

The Apache Web server can manipulate various information through the.htaccess file, which is the default name of a directory level configuration file, allowing centralized Web server configuration management. Can be used to override the global configuration of the server. The purpose of this file is to allow access control configurations for individual directories, such as passwords and content access.

1. Custom directory Index files

DirectoryIndex index.html index.php index.htm

You can use the above configuration to change the default page of the directory. For example, if you put this script in the foo directory, the user will access /foo/ index.html when they request /foo/.

2. Custom error pages

ErrorDocument 404 errors/404.html

When a user visits a page and reports an error, such as if the page cannot find a custom error page that you want to display, you can do so this way. Or dynamic pages:
ErrorDocument 404 /psych/cgi-bin/error/error?404

3. Control the level of access to files and directories

.htaccess is often used to restrict and deny access to certain files and directories. For example, we have a folder called includes, where we store some scripts.


# no one gets in here!
deny from all
The above script is to deny all access you can also follow IP Duan lai refused:
# no nasty crackers in here!
order deny,allow
deny from all
allow from 192.168.0.0/24
# this would do the same thing..
#allow from 192.168.0

Generally these methods are handled through a firewall, but in a production environment for the server, such adjustment is very convenient.
Sometimes you just want to block access to an ip:
# someone else giving the ruskies a bad name..
order allow,deny
deny from 83.222.23.219
allow from all

4. Modify environment variables

The environment variable contains some information about the server-side CGI extension, which can be set and unset using SetEnv and UnSetEnv.

SetEnv SITE_WEBMASTER "Jack Sprat"
SetEnv SITE_WEBMASTER_URI mailto:Jack.Sprat@characterology.com
     
UnSetEnv REMOTE_ADDR

5. 301 redirection

If you want a page to jump to a new one:

Redirect 301 /old/file.html http://yourdomain.com/new/file.html

The entire path can be redirected as follows:
RedirectMatch 301 /blog(.*) http://yourdomain.com/$1

6. Implement the caching policy through.htaccess

Caching static files on the browser can improve the performance of your site:

# year
<FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|swf|mp3|mp4)$">
Header set Cache-Control "public"
Header set Expires "Thu, 15 Apr 2010 20:00:00 GMT"
Header unset Last-Modified
</FilesMatch>
#2 hours
<FilesMatch "\.(html|htm|xml|txt|xsl)$">
Header set Cache-Control "max-age=7200, must-revalidate"
</FilesMatch>
<FilesMatch "\.(js|css)$">
SetOutputFilter DEFLATE
Header set Expires "Thu, 15 Apr 2010 20:00:00 GMT"
</FilesMatch>

7. Use GZIP to compress the output

All css, js, and html are compressed using the GZIP algorithm by adding the following code to.htaccess:

<IfModule mod_gzip.c>
    mod_gzip_on       Yes
    mod_gzip_dechunk  Yes
    mod_gzip_item_include file      \.(html?|txt|css|js|php|pl)$
    mod_gzip_item_include handler   ^cgi-script$
    mod_gzip_item_include mime      ^text/.*
    mod_gzip_item_include mime      ^application/x-javascript.*
    mod_gzip_item_exclude mime      ^image/.*
    mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</IfModule>

You can use the following script to determine whether the Web server provides mod_deflate support:

ErrorDocument 404 errors/404.html
0
If the Web server does not support mod_deflate, use the following method:
ErrorDocument 404 errors/404.html
1

8. Mandatory access by HTTPS

The following script can be used to force the entire website to be accessed using https:

RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

9. URL rewritten

For example, product. php? id=12 is rewritten as es1064en-12.html

RewriteEngine on
RewriteRule ^product-([0-9]+)\.html$ product.php?id=$1

Will product. php & # 63; id=12 is rewritten as product/ ipod-nano / 12.html
ErrorDocument 404 errors/404.html
4
Redirection without www to URL address with www:
ErrorDocument 404 errors/404.html
5
Rewrite yoursite. com/user. php & # 63; username = xyz to yoursite. com/xyz
ErrorDocument 404 errors/404.html
6
Redirect a domain name to a new subfolder in public_html:
ErrorDocument 404 errors/404.html
7

10. Block listing of directory files

To prevent all files in the list directory, use the following code:

ErrorDocument 404 errors/404.html
8
or
ErrorDocument 404 errors/404.html
9

11. Add a new MIME-Types

MIME-types depends on the file extension, and unrecognized file extensions are transmitted as text data

ErrorDocument 404 /psych/cgi-bin/error/error?404
0

12. Hotlinking prevention

You do not want other people's website to quote your site pictures, css and other static files, that is, the legend of anti-hotlinking, you can use the following script:

RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{REQUEST_URI} !^/(wp-login.php|wp-admin/|wp-content/plugins/|wp-includes/).* [NC]
RewriteCond %{HTTP_REFERER} !^http://www.askapache.com.*$ [NC]
RewriteRule \.(ico|pdf|flv|jpg|jpeg|mp3|mpg|mp4|mov|wav|wmv|png|gif|swf|css|js)$ - [F,NS,L]

13. Specify the size limit for the uploaded file, which applies to PHP


php_value upload_max_filesize 20M
php_value post_max_size 20M
php_value max_execution_time 200
php_value max_input_time 200

In the above script, the upload limits are set by four parameters: the first parameter is the size of the file, the second is the size of the POST data, the third is the transfer time (unit seconds), and the last one is the maximum time spent parsing the uploaded data (unit seconds).

14. Script execution is prohibited

Options -ExecCGI
AddHandler cgi-script .php .pl .py .jsp .asp .htm .shtml .sh .cgi

15. Modify character set and language header


AddDefaultCharset UTF-8
DefaultLanguage en-US

16. Set the server time zone (GMT)

SetEnv TZ America/Indianapolis

17. Force "File Save As" prompt

AddType application/octet-stream .avi .mpg .mov .pdf .xls .mp4

18. Protect individual files

Normally.htaccess can be used to restrict access to the entire directory, but you can also restrict access to a single file:

ErrorDocument 404 /psych/cgi-bin/error/error?404
7

19. Set Cookie

Set Cookie with an environment variable

ErrorDocument 404 /psych/cgi-bin/error/error?404
8
Setting Cookie based on the request, the code sends the Set-Cookie header to set the Cookie value as the match in the second bracket

RewriteEngine On
RewriteBase /
RewriteRule ^(.*)(de|es|fr|it|ja|ru|en)/$ - [co=lang:$2:.yourserver.com:7200:/]

20. Set the custom response Headers


# no one gets in here!
deny from all
The above script is to deny all access you can also follow IP Duan lai refused:
# no nasty crackers in here!
order deny,allow
deny from all
allow from 192.168.0.0/24
# this would do the same thing..
#allow from 192.168.0
0

21. Block requests according to User-Agent


# no one gets in here!
deny from all
The above script is to deny all access you can also follow IP Duan lai refused:
# no nasty crackers in here!
order deny,allow
deny from all
allow from 192.168.0.0/24
# this would do the same thing..
#allow from 192.168.0
1


Related articles: