实用.htaccess用法大全

这里收集的是各种实用的 .htaccess 代码片段,你能想到的用法几乎全在这里。

免责声明: 虽然将这些代码片段直接拷贝到你的.htaccess文件里,绝大多数情况下都是好用的,但也有极个别情况需要你修改某些地方才行。风险自负。

重要提示: Apache 2.4 有不兼容的修改,特别是在访问配置控制方面。详细信息请参考这篇更新文档以及这篇文章。

目录:重新和重定向

注意:首先需要服务器安装和启用mod_rewrite模块。

强制 wwwRewriteEngine onRewriteCond %{HTTP_HOST} ^example\.com [NC]RewriteRule ^(.*)$ $1 [L,R=301,NC]强制 www通用方法RewriteCond %{HTTP_HOST} !^$RewriteCond %{HTTP_HOST} !^www\. [NC]RewriteCond %{HTTPS}s ^on(s)|RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

这种方法可以使用在任何网站中。Source

强制 non-www

究竟是WWW好,还是non-www好,,没有定论,如果你喜欢不带www的,可以使用下面的脚本:

RewriteEngine onRewriteCond %{HTTP_HOST} ^www\.example\.com [NC]RewriteRule ^(.*)$ $1 [L,R=301]强制 non-www通用方法RewriteEngine onRewriteCond %{HTTP_HOST} ^www\.RewriteCond %{HTTPS}s ^on(s)|offRewriteCond http%1://%{HTTP_HOST} ^(https?://)(www\.)?(.+)$RewriteRule ^ %1%3%{REQUEST_URI} [R=301,L]强制 HTTPSRewriteEngine onRewriteCond %{HTTPS} !onRewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}# Note: It’s also recommended to enable HTTP Strict Transport Security (HSTS) # on your HTTPS website to help prevent man-in-the-middle attacks.# See https://developer.mozilla.org/en-US/docs/Web/Security/HTTP_strict_transport_security<IfModule mod_headers.c>Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains"</IfModule>强制 HTTPS 通过代理

如果你使用了代理,这种方法对你很有用。

RewriteCond %{HTTP:X-Forwarded-Proto} !httpsRewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}强制添加末尾斜杠RewriteCond %{REQUEST_URI} /+[^\.]+$RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]取掉末尾斜杠RewriteCond %{REQUEST_FILENAME} !-dRewriteRule ^(.*)/$ /$1 [R=301,L]重定向到一个页面Redirect 301 /oldpage.html Redirect 301 /oldpage2.html

Source

目录别名RewriteEngine OnRewriteRule ^source-directory/(.*) target-directory/$1脚本别名FallbackResource /index.fcgi

This example has anindex.fcgifile in some directory, and any requests within that directory that fail to resolve a filename/directory will be sent to theindex.fcgiscript. It’s good if you wantbaz.foo/some/cool/pathto be handled bybaz.foo/index.fcgi(which also supports requests tobaz.foo) while maintainingbaz.foo/css/style.cssand the like. Get access to the original path from the PATH_INFO environment variable, as exposed to your scripting environment.

RewriteEngine OnRewriteRule ^$ index.fcgi/ [QSA,L]RewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-dRewriteRule ^(.*)$ index.fcgi/$1 [QSA,L]

This is a less efficient version of the FallbackResource directive (because usingmod_rewriteis more complex than just handling theFallbackResourcedirective), but it’s also more flexible.

重定向整个网站Redirect 301 /

This way does it with links intact. That iswill become. This is extremely helpful when you are just “moving” a site to a new domain.Source

干净的URL

This snippet lets you use “clean” URLs — those without a PHP extension, e.g.example.com/usersinstead ofexample.com/users.php.

RewriteEngine OnRewriteCond %{SCRIPT_FILENAME} !-dRewriteRule ^([^.]+)$ $1.php [NC,L]

Source

Security拒绝所有访问## Apache 2.2Deny from all## Apache 2.4# Require all denied

But wait, this will lock you out from your content as well! Thus introducing…

拒绝所有访问(排除部分)## Apache 2.2Order deny,allowDeny from allAllow from xxx.xxx.xxx.xxx## Apache 2.4# Require all denied# Require ip xxx.xxx.xxx.xxx

xxx.xxx.xxx.xxxis your IP. If you replace the last three digits with 0/12 for example, this will specify a range of IPs within the same network, thus saving you the trouble to list all allowed IPs separately.Source

Now of course there’s a reversed version:

屏蔽爬虫/恶意访问## Apache 2.2Order deny,allowAllow from allDeny from xxx.xxx.xxx.xxxDeny from xxx.xxx.xxx.xxy## Apache 2.4# Require all granted# Require not ip xxx.xxx.xxx.xxx# Require not ip xxx.xxx.xxx.xxy保护隐藏文件和目录

Hidden files and directories (those whose names start with a dot.) should most, if not all, of the time be secured. For example:.htaccess,.htpasswd,.git,.hg…

RewriteCond %{SCRIPT_FILENAME} -d [OR]RewriteCond %{SCRIPT_FILENAME} -fRewriteRule "(^|/)\." – [F]

Alternatively, you can just raise aNot Founderror, giving the attacker dude no clue:

RedirectMatch 404 /\..*$保护备份文件和源代码文件捕捉最后的流星,坐在最高的山顶上,可以听音乐,聊电影,

实用.htaccess用法大全

相关文章:

你感兴趣的文章:

标签云: