Apache 在表单参数上使用mod_rewrite

<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
 
RewriteCond %{REQUEST_URI} /search.php$
RewriteCond %{QUERY_STRING} ^q=([A-Za-z0-9\+]+)&s=Search$
RewriteRule ^(.*)$ /s-%1? [R=301,L]
 
RewriteRule ^s-(.*)$ /search.php?q=$1&s=Search&a=1 [L]
</IfModule>

Apache 禁用电子标签

Add the following line to your .htaccess:

FileETag none

Apache Gzip您的组件

### For Apache 1 ###

mod_gzip_on Yes 

mod_gzip_item_include mime ^application/x-javascript$ 
mod_gzip_item_include mime ^application/json$ 
mod_gzip_item_include mime ^text/.*$ 

mod_gzip_item_include file \.html$ 
mod_gzip_item_include file \.php$ 
mod_gzip_item_include file \.js$ 
mod_gzip_item_include file \.css$ 
mod_gzip_item_include file \.txt$ 
mod_gzip_item_include file \.xml$ 
mod_gzip_item_include file \.json$ 

Header append Vary Accept-Encoding

### For Apache 2 ###

AddOutputFilterByType DEFLATE text/html text/css text/plain text/xml application/x-javascript application/json
Header append Vary Accept-Encoding

Apache 域重定向转发

Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]

Apache 常见.htaccess重写规则

## no-www redirect
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L] 

## www redirect
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

## multiple domain redirect
RewriteCond %{HTTP_HOST} !^example\.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]


## trailing slash
## BE EXTRA CAREFUL WITH THIS ONE!
## POST REQUESTS WITHOUT TRAILING SLASH
## WILL BE IGNORED!
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://www.example.com/$1/ [L,R=301]

Apache 防止热线链接

RewriteEngine On 

## PREVENT HOTLINKING ###
SetEnvIfNoCase Cookie SESSIONNAME=(.*) IS_OK
SetEnvIfNoCase Referer "^http://(www\.)?example\.com(/.*)?$" IS_OK
#SetEnvIfNoCase Referer "^http://(www\.)?example\.net(/.*)?$" IS_OK

<FilesMatch "\.(avi|bmp|flv|gif|jpe?g|mov|mp(3|4|e?g)|png|swf|tiff?|wm(a|v))$">
  Order deny,allow
  deny from all
  allow from env=IS_OK
  
  ErrorDocument 403 http://127.0.0.1/
</FilesMatch>

Apache 来自文件的虚假文件夹

RewriteEngine On
RewriteBase /
RewriteRule ^admin/.*$ - [PT]
# Use the above line for a folder that you DON'T want to follow these rules
RewriteRule ^/?([a-zA-Z0-9-_/]+)/$ $1.php [L]
# Here's where the "magic" happens
# http://www.example.com/page.php - OLD
# http://www.example.com/page/ - NEW

Apache 阻止不良请求

RewriteCond %{REQUEST_METHOD} !^(GET|HEAD|POST|PROPFIND|OPTIONS|PUT)$ [NC]
RewriteRule .* - [F,NS,L]

Apache apache2 - 启用和禁用站点

# enable site
sudo a2ensite

# disable site
sudo a2dissite

# enable an apache2 module
sudo a2enmod

# e.g. a2enmod php4 will create the correct symlinks in mods-enabled to allow the module to be used. In this example it will link both php4.conf and php4.load for the user

# disable an apache2 module
sudo a2dismod

# force reload the server:
sudo  /etc/init.d/apache2 force-reload

Apache 强化Wordpress

<Directory "/example/htdocs/wp-content/uploads/">
        php_admin_flag engine off
    </Directory>
    <Directory "/example/htdocs/wp-content/themes/">
        <Files *php>
            Order allow,deny
            Deny from all
        </Files>
    </Directory>