htaccess详解:URL重写与重定向
URL重定向是.htaccess的重头戏,它可以将长地址转为短地址、将动态地址转为静态地址、重定向丢失的页面、防止盗链、实现自动语言转换等。笔者觉得难点是在正则表达式的运用和理解上。
准备开始:mod_rewrite
实现所有这些神奇功能的模块叫做mod_rewrite,请确保你的服务器安装并启用了该模块:
sudo a2enmod rewrite
我们一般会把所有涉及URL重写或者重定向的代码这样放置:
<IfModule mod_rewrite.c> # Turn on rewrite engine Options +FollowSymlinks RewriteEngine on # More rules below ... </IfModule>
一些我们需要注意的地方:
- FollowSymlinks必须启用,这是rewrite引擎的安全需求。
- 通常FollowSymlinks在Apache的主配置文件中就已经启用了,所以通常可以省略。
- RewriteEngine命令用于启用rewrite引擎
- IfModule命令用于判断Apache是否安装了mod_rewrite模块,之后笔者会省略该命令,但不代表这是个好习惯。
- mod_rewrite会处理所有提交给Apache的URL请求,并与之后的规则进行匹配
下面我们开始讲解一些例子。
利用.htaccess实现URL重写(rewrite)与URL重定向(redirect)
将.htm页面映射到.php
Options +FollowSymlinks RewriteEngine on RewriteRule ^(.*).htm$ $1.php [NC]
注意事项:
- 该RewriteRule能够将.htm静态页面映射到.php动态页面
- 如果通过.htm进入,浏览器地址栏显示的是.htm扩展名,但服务器上实际执行的是.php
- 必须保证服务器上有对应的.php,否则会404
- 浏览器和搜索引擎可以同时通过.htm和.php访问网页
- 如果该目录上存在.htm,将被忽略
- [NC]表示“不区分大小写”
临时重定向(R=302)与永久重定向(R=301)
RewriteEngine on RewriteBase / RewriteRule ^(.*).htm$ $1.php [R,NC,L]
注意事项:
- 该RewriteRule能够将.htm静态页面重定向到.php动态页面
- 如果通过.htm进入,浏览器地址栏会自动转为.php,这也是重定向的本质
- 必须保证服务器上有对应的.php,否则会404
- 浏览器和搜索引擎可以同时通过.htm和.php访问网页
- 如果该目录上存在.htm,将被忽略
- RewriteBase定义了重写基准目录。
- 例如,如果你将虚拟站点设置在/var/www目录下,删除这行将会导致重定向到http://yourdomain.com/var/www/1.php。显然这是找不到的,而且你也不会希望用户看见你的服务器的目录结构。
- 再举个例子,如果RewriteBase /base/,那么将会重定向到http://yourdomain.com/base/1.php。
- 对于重写基准目录,我们还可以通过将$1.php变成/$1.php实现直接变换,这时就可以将RewriteBase省略。
- 字母R表示临时重定向,相当于[R=302,NC]。
- 字母L表示如果能匹配本条规则,那么本条规则是最后一条(Last),忽略之后的规则。
在讨论R=302临时重定向后,理解R=301永久重定向也就容易多了:
RewriteEngine on RewriteRule ^(.*)$ http://newdomain.com/$1 [R=301,NC,L]
- 这个规则告诉浏览器和搜索引擎,网站地址发生了永久性变更,用户的URL请求将会被发送给新的域名(主机)处理。
- 由于是重定向到新的主机地址,RewriteBase也就没有出现的必要了。
为什么要用重定向?——重定向和URL重写的区别
- 通过重定向,浏览器知道页面位置发生变化,从而改变地址栏显示的地址
- 通过重定向,搜索引擎意识到页面被移动了,从而更新搜索引擎索引,将原来失效的链接从搜索结果中移除
- 临时重定向(R=302)和永久重定向(R=301)都是亲搜索引擎的,是SEO的重要技术
- URL重写用于将页面映射到本站另一页面,若重写到另一网络主机(域名),则按重定向处理
长短地址转换
利用URL重写,我们可以很方便地实现长短地址的转换,但是用重定向就不合适了。
RewriteEngine On RewriteRule ^grab /public/files/download/download.php
若访问
http://mysite/grab?file=my.zip
则会执行该页面:
http://mysite/public/files/download/download.php?file=my.zip
去掉www
Options +FollowSymlinks RewriteEngine on RewriteCond %{HTTP_HOST} ^www.(.*) [NC] RewriteRule ^(.*)$ http://%1/$1 [R=301,NC,L]
加上www
RewriteEngine On RewriteCond %{HTTP_HOST} ^(.*)$ RewriteRule (.*) http://www.%1/$1 [R=301,L]
支持多域名访问
如果你不凑巧买到了不支持多域名的主机,那么.htaccess或许可以帮助你。现在假设你有域名domain-one.com和domain-two.com,并且在服务器根目录有对应文件夹one和two,那么通过下面的改写就能让Apache同时接受者两个域名的请求:
#two domains served from one root.. RewriteCond %{HTTP_HOST} domain-one.com RewriteCond %{REQUEST_URI} !^/one RewriteRule ^(.*)$ /one/$1 [L] RewriteCond %{HTTP_HOST} domain-two.com RewriteCond %{REQUEST_URI} !^/two RewriteRule ^(.*)$ /two/$1 [L]
改写查询字符串QUERY_STRING
查询字符串是指URL请求中“问号”后面的部分。比如,http://mysite/grab?foo=bar中粗体部分就是查询字符串,其中变量名是foo,值是bar。
利用QSA转换查询字符串QUERY_STRING
QSA标志( Query String Appending)用于在URI中截取查询字符串,这个截取操作是通过小括号正则表达式实现的:
RewriteEngine On RewriteRule /pages/(.+) /page.php?page=$1 [QSA]
- 将会把请求/pages/123?one=two 映射到 /page.php?page=123&one=two
- 注意粗体部分几乎是相同的,除了“问号”变成了“与”符号
- 如果没有QSA标志,那么会映射到/page.php?page=123。
- 如果没有用到小括号正则表达式,就不需要QSA,这在上节“长短地址转换”中已经例证过了。
- 小括号正则表达式可以截取查询字符串中的内容,但是如果没有开启QSA标志,那么在/page.php?page=$1中“问号”之后将会被剥离丢弃。这种特性可以用于实现“剥离查询字符串”
通过QSA,我们可以将简单链接/simple/flat/link/ 映射成 server-side.php?first-var=flat&second-var=link
RewriteEngine On RewriteRule ^/([^/]+)/([^/]+)/? /index.php?first-var=$1&second-var=$2 [QSA]
利用RewriteCond改写查询字符串QUERY_STRING
RewriteEngine On RewriteCond %{QUERY_STRING} foo=(.*) RewriteRule ^grab(.*) /page.php?bar=%1
- 该规则将访问请求http://mysite/grab?foo=bar转换为http://mysite/page.php?bar=bar
- RewriteCond用于捕获查询字符串(QUERY_STRING)中变量foo的值,并存储在%1中
- QUERY_STRING是Apache定义的“变量=值”向量(数组)
QSA与RewriteCond双剑齐发
RewriteEngine On RewriteCond %{QUERY_STRING} foo=(.+) RewriteRule ^grab/(.*) /%1/index.php?file=$1 [QSA]
- 会把/grab/foobar.zip?level=5&foo=bar 映射到 /bar/index.php?file=foobar.zip&level=5&foo=bar
- 转换后根目录是bar目录
- foobar.zip?level=5中的“问号”变成了foobar.zip&level=5中的“与”符号
剥离查询字符串
只需在要开始剥离的链接后面加个“问号”,并且不要启用QSA标志,就可剥离查询字符串
RewriteEngine On # Whatever QS is RewriteCond %{QUERY_STRING} . # I don't want it with Question mark RewriteRule foo.php(.*) /foo.php? [L]
利用RewriteCond和RewriteRule进行访问控制
我们在第一篇.htaccess基础中提到了很多有用的访问控制方法,其实通过Rewrite也能实现类似的功能,而且可以更强大!
文件访问控制
之前利用Order、Files及FilesMatch命令实现的访问控制可以满足大部分要求,但是当用户被拒绝时,他们看到的是硕大的“403 Forbidden”,如果你不想伤害用户的感情,就需要显示一些别的东西,通过Rewrite就可以实现这个特性:
RewriteEngine On RewriteCond %{REQUEST_FILENAME} !^(.+).css$ RewriteCond %{REQUEST_FILENAME} !^(.+).js$ RewriteCond %{REQUEST_FILENAME} !special.zip$ RewriteRule ^(.+)$ /chat/ [NC]
- 该规则将仅允许用户请求.css, .js类型的文件,还有special.zip文件
- RewriteRule 后面指定了限制规则:映射到/char/目录下处理
- RewriteCond 后面的“感叹号”(!)起到了“否定”作用,它表明,对不满足后面正则表达式者应用RewriteRule规则,也就是对当前类型的文件将不应用规则
- RewriteCond 之间是以逻辑“与”连接的,也就是只有当三个条件都不满足时才执行RewriteRule
- 该规则也会限制访问.htm, .jpg等格式
- 该规则不可以放在虚拟站点根目录(/)下,否则会死循环
- 如果是二级目录,如/test/,那么传入RewriteCond的参数是以/test/开始的,因此从(.+)获得的文件名也含有/test/,读者必须对此多加小心
- 要想仅获得文件名,可以将(.+)替换成([^/]+),并且去掉符号^,如下所示:
RewriteEngine On RewriteCond %{REQUEST_FILENAME} !([^/]+).css$ RewriteCond %{REQUEST_FILENAME} !([^/]+).js$ RewriteRule ^(.+)$ /chat/ [NC]
用.htaccess阻止User-agent
什么是User-agent?User-agent用于浏览器向服务器“自报家门”,更确切的说是所有HTTP客户端都得用User-agent向服务器“自报家门”,以便服务器对不同的客户端作出不同响应。比如,某站点可能需要对浏览器、搜索引擎crawl还有各类下载工具作出不同的响应。服务器就是通过所谓的User-agent进行区分的。
如果你的服务器提供某些资源的下载,那么你就必须多加小心诸如“迅雷”等下载软件,因为它们可能把你网站资源吸干,并且影响你的正常访客访问。为此,我们可以利用Rewrite限制某些UA的访问:
RewriteEngine on RewriteCond %{HTTP_USER_AGENT} 2.0.50727 [NC] RewriteRule . abuse.txt [L]
- 该规则限制“迅雷”客户端下载资源,并将下载文件重置到abuse.txt
- HTTP_USER_AGENT是Apache的内置变量
- 2.0.50727是迅雷User-agent的特征字符串
- RewriteRule后面的“点”表示“任意URI”,也就是不管请求的是什么,都输出abuse.txt
通常,我们不会仅限制一个UA。利用[OR]即可实现对多个UA作出统一处理:
RewriteEngine on RewriteCond %{HTTP_USER_AGENT} 2.0.50727 [NC,OR] RewriteCond %{HTTP_USER_AGENT} ^BlackWidow [NC,OR] # etc.. RewriteCond %{HTTP_USER_AGENT} ^Net Vampire [NC] RewriteRule . abuse.txt [L]
用.htaccess阻止盗链(hot-linking)
盗链,特别是图片,是非常可耻的!哪怕将图片复制到自己服务器上,也比盗用他人的图片链接来得光彩!(吐糟完毕)
.htaccess的Rewrite功能可以提供非常简单、有效的方法阻止这种可耻行为:
RewriteEngine On RewriteCond %{HTTP_REFERER} !^$ RewriteCond %{HTTP_REFERER} !^http://(www.)?52fisher.cn/ [NC] RewriteCond %{REQUEST_URI} !hotlink.png [NC] RewriteRule .*.(gif|jpg|png)$ /hotlink.png [NC]
简单解释一下该规则的功能:
- 除本站以外其他网站都不得引用本站图片,具体可以理解为
- 如果引用站点为“空”或者是“本站”,或者,所引用对象是“hotlink.png”,那么就允许访问
- 再次提醒,RewriteCond之间默认的逻辑连接词是逻辑“与”
- 这里的难点是理解逻辑转换,即德·摩根定律
第一借錢
https://168cash.com.tw/
第一借錢
https://168cash.com.tw/
第一借錢
https://168cash.com.tw/
陈夥蚧:文章真不错https://www.vancii.com/tyt/6859.html
#3僱用一名殺手 名刺客 - 您可以在我們的網站上下令謀殺您不喜歡的人。我們的殺手可以開槍、刺傷人,甚至模擬街頭搶劫。如何毒害一個人而不留痕跡?下令綁架。命令毆打一個人。
#3https://chinese-hitman-assassin.com/
现在,世界各地的任何人都可以获得额外的收入。 https://t.me/cryptaxbot/17?SpoxOnee62Vaw
哇塞! 这个机器人是在线职业生涯的一个很好的开始。] https://t.me/cryptaxbot/17?SpoxOnee11Vaw
金融机器人是有史以来第一个投资工具. 发射它! https://t.me/cryptaxbot/17?SpoxOnee57Vaw
需要钱吗? 金融机器人是你的解决方案。] https://t.me/cryptaxbot/17?SpoxOnee45Vaw
#网络金融专家! 看看新的机器人。 https://t.me/cryptaxbot/17?SpoxOnee26Vaw
让你钱包变厚的最快方法就在这里。] https://t.me/cryptaxbot/17?SpoxOnee95Vaw
看看自动机器人,它为你工作24/7。] https://t.me/cryptaxbot/17?SpoxOnee16Vaw
#网络金融专家! 看看新的机器人。 https://t.me/cryptaxbot/17?SpoxOnee99Vaw
注意! 在这里,您可以在线赚钱!] https://t.me/cryptaxbot/14?SpoxOnee86Vaw
没有投资的巨额收入是可用的,现在!] https://t.me/cryptaxbot/14?SpoxOnee67Vaw
即使你睡觉,金融机器人也为你工作。 https://t.me/cryptaxbot/14
让你的钱成长为资本与这个机器人。 https://t.me/cryptaxbot/14
期待收入? 上网吧。 https://t.me/cryptaxbot/14
找出最简单的赚钱方法。 https://t.me/cryptaxbot/11
与我们一起保护您的权利!
从经验丰富的律师那里获得专业的法律建议。
访问我们的律师网站,安排今天的会议.
您的安全与和平是我们的首要任务!
Our website https://adm-tigin.ru
您是否需要google上的评论,投票网站上的评论,您是否需要有人创建gmail帐户,您是否希望增加youtube视频的浏览量,或者您是否需要人们在互联网上执行小任务, https://aviso.bz/?r=filosof20063
通过观看新视频轻松赚钱youtube.com https://aviso.bz/?r=filosof20063
通过观看新视频轻松赚钱youtube.com https://aviso.bz/?r=filosof20063
冠天下
https://xn--ghq10gmvi.com/
teen cam
amoxicillin
tadalafil
pornhub virtual sex
ventolin
buy celebrex
order ventolin
generic valtrex
cialis canada
zithromax 500
online sex chat
amoxicillin tablets for sale
sildenafil
sex chat cam
buy lasix
buy metformin
buy propecia
valtrex buy online
girl cam
tadalafil tablets 20mg
chat avenue.com
propecia hair loss
doxycycline hyclate 100mg price
furosemide 12.5 mg
motilium
zithromax 250 mg
prednisone 5mg
order acyclovir
valtrex cheap
buy online 10mg prednisolone
adult cams
lasix
interactive porn
buy predislone tablets
webcam girls
interactive sex
porn live cams
adult video chat
online fucking
zestril lisinopril
free webcam chat
how to buy synthroid online with no prescription
buy valtrex without get a prescription online
sildenafil
video chat room
buy propecia
sexcamslive
prednisone
doxycycline 50mg
buy acyclovir online
online prescription free zoloft
motilium
interactive sex
free live cam
sex cam chat
propecia buy without per
amoxicillin cost
order synthroid without prescription
free gay chat rooms
generic drug for lisinopril
prednisone without a prescription
free adult cams
adult sex chat
free cam girls
incestflix virtual sex
freechatnow.com
webcam girls
adult chat
adult chat rooms
free adult chat rooms
gay web cam chat
robaxin
where can i buy prednisone
cam dolls
synthroid 50
free sex cam
stromectol
webcam sex
amateur webcam sex
mature cams
free webcam girls
chat rooms yahoo
doxycycline
cam com.goodass cam girl
doxycycline 100mg tablets
amoxicillin without a prescription
whoagirls
tadalafil 5mg
doxycycline vibramycin
teen cam
buy metformin er online without prescription
cialis canada
amateur cams
free adult chat room
incest chat rooms
livegirls
tadalafil
mature live
robaxin online
cam dolls
cialis canada
no petscriptions zoloft
propecia buy
cam girl
20mg cialis
ivermectin for sale
buy generic viagra online
buy generic viagra online
adultchatroom
doxycycline 100mg tablets no prescription
adult cams
synthroid 50mcg
female viagra
best auto insurance
sildenafil women
direct general auto
auto insurance online
auto insurance
auto insurance
purchase amoxicillin 500 mg
female viagra 100mg
auto insurance quote
valtrex
buy cheap viagra online
buy cheap viagra online
lasix price
valtrex
motilium
amoxicillin
buy cheap viagra online
buy cheap viagra online
buy cheap viagra online
buy cheap viagra online
lasix
sildenafil
female pink viagra
compare auto insurance quotes
motilium
auto insurance
buy synthroid
motilium tablets
synthroid
motilium 10mg
motilium
buy synthroid
buy female viagra online
motilium tablets
200 mg doxycycline
buy synthroid without a prescription
valtrex
amoxicillin 500 mg capsules
synthroid
buy motilium
lasix
compare auto insurance quotes
synthroid
buy finasteride
synthroid
motilium domperidone 10mg
synthroid generic
valtrex 500 mg
auto insurance
auto insurance
auto insurance quote online
synthroid
motilium cvs
auto insurance
propecia
motilium domperidone 10mg
buy motilium online
car insurance comparison quotes
doxycycline hyclate
sildenafil
order female viagra online
buy synthroid
buy sildenafil
propecia
auto insurance quotes comparison
tadalafil cialis
doxycycline hyclate 100 mg cap
sildenafil buy
female viagra
tadalafil
auto insurance
amoxicillin 500
propecia
valtrex valacyclovir
motilium
car auto insurance quotes
propecia buy
buy motilium online
buy motilium
over the counter sildenafil
female viagra buy
insurance for young drivers
sildenafil
motilium
furosemide lasix
compare auto insurance rates
where can i buy cialis tadalafil in edmonton
female viagra online
female viagra
auto insurance
female viagra
motilium otc
buy motilium online
amoxicillin 500 mg online
500 mg amoxicillin
amoxicillin 500mg capsules
auto insurance
american collectors insurance
buy cheap doxycycline
buy propecia finasteride
doxycycline 100mg tablets
amoxicillin
valtrex
buy sildenafil citrate
female viagra
buy cheap lasix
buy amoxicillin 500mg
auto insurance
generic cialis tadalafil
synthroid
sildenafil citrate india
can i buy amoxicillin over the counter
furosemide lasix
motilium
usaa insurance
buy motilium online
auto insurance
auto insurance quotes comparison
sildenafil
buy valtrex online without a prescription
buy tadalafil cialis
doxycycline hyc 100mg
buy motilium
american collectors insurance
female viagra
best auto insurance
auto insurance
buy generic cialis online
buy generic cialis online
motilium domperidone 10mg
lasix
valtre
buy generic valtrex without prescription
amoxicillin 500 mg
valtrex
tadalafil
auto insurance
tadalafil
female viagra buy online
propecia for less
http://viagenericahecv.com - online pharmacy india viagra prescription online online pharmacy viagra
buy amoxicilina 500 mg
temporary car insurance quote
http://erectionjaen.com - precription drugs from canada online viagra п»їgeneric viagra
amoxicillin online no prescription
buy furosemide
auto insurance providers
amoxicillin pill
auto insurance
motilium
doxycycline
valtrex buy
synthroid
auto insurance
lasix
generic motilium
motilium domperidone 10mg
lasix
buy valrex online
doxycycline
http://buyjeacialonline.com/ - when will viagra go generic 100mg viagra best place to buy generic viagra online
buy sildenafil online
propecia buy
auto insurance online
compare auto insurance quotes
amoxicillin
http://genericcilaken.com/ - how to buy cialis viagra 100mg canada drugs online
auto insurance
http://cialiscials.com - canadian drug stores buy discount viagra canada drugs online reviews
generic sildenafil
propecia .5 mg
auto insurance
propecia buy
amoxicillin buy online
tadalafil generic cialis
tadalafil
auto insurance quotes comparison
buy propecia
https://saresltd.com - where can i buy cialis viagra plus buy cialis online safely
auto insurance quotes online florida
synthroid
motilium
purchase synthroid
motilium tablets
tadalafil
doxycycline
cialis generic tadalafil
https://saresltd.com/clomid/ - cialis buy online cheap viagra online canada drugs online review
motilium
where to buy sildenafil
valtrex prescription
owners insurance
doxycycline 200 mg
motilium
amoxicillin over counter
synthroid
auto insurance
cheap lasix online
sildenafil
http://valcialisns.com/ - does generic viagra work viagra use canadian online pharmacy
auto insurance quote online
doxycycline
aarp car insurance for seniors
insurance companies
tadalafil generic
amoxicillin
generic tadalafil 20mg
valtrex
http://viagraveikd.com/# - prescription drugs online viagra for sale best canadian pharmacy
synthroid
amoxicillin 500 mg without prescription
sildenafil 100 mg
tadalafil
amoxicillin
auto insurance quotes online
female viagra buy
sildenafil
auto insurance quotes online
sildenafil
auto insurance quotes online
tadalafil generic cialis
sildenafil generic
sildenafil women
valtrex
auto insurance quote
propecia
valtrex
buy amoxicillin 500mg
auto insurance
https://saresltd.com/cialis/ - buying prescription drugs from canada generic viagra 100mg prescription drugs online
motilium tablets
car insurance in florida
travelers auto insurance
http://cialisgretkjss.com/# - canadian drug stores online cialis best place to buy cialis online reviews
lipitor 20 mg
auto insurance quotes comparison
https://saresltd.com/kamagra/ - walmart pharmacy online viagra online uk canada drugs online
seroquel 50 mg
auto insurance online
http://menedkkr.com/# - when will viagra go generic viagra brand name canadian drug stores
seroquel 50 mg
lisinopril 5mg
albuterol inhaler for sale
trazodone 75 mg
http://viagranerrds.com/# - buy cialis canada generic viagra online canadian pharmacies online
aciclovir cream
lipitor 10 mg
seroquel
seroquel
auto insurance
auto insurance quotes comparison
ventolin hfa inhaler
http://viagrapfhze.com/# - trusted online pharmacy reviews viagra spam canada drugs reviews
acytoclovir 800
auto insurance
cheap acyclovir
lisinopril 20mg tablets
auto owners insurance co
discount prednisolone
auto insurance
40 mg lisinopril
auto insurance quotes
buy acyclovir online
http://cialisovnnc.com/# - modafinil online pharmacy cialis canada viagra generic date
auto insurance
buy trazodone
auto insurance
seroquel
auto insurance
auto insurance
compare auto insurance rates
buy trazodone
buy ventolin inhaler online
lisinopril hct
ventolin
albuterol
lisinopril 40 mg
all state auto insurance
http://viagracefo.com/# - is there a generic for viagra order viagra canadian pharmacy viagra
acyclovir 800 online no rx
auto insurance
auto insurance quotes
trazodone
auto insurance quote online
trazodone
albuterol inhaler
trazodone hcl 50mg
http://bestbuymaleenhancement.com/# - buy generic cialis viagra suppliers online canadian pharmacy
albuterol
lisinopril 10mg
acyclovir
seroquel
auto insurance quotes comparison
elephant insurance
compare auto insurance quotes
acyclovir capsules
temporary car insurance quote
auto insurance coverage
drug lisinopril
acyclovir 800mg
albuterol inhaler for sale
seroquel xr 50mg
what is keto diet plan best foods for keto diet keto diet bread recipes
seroquel
acyclovir
ventolin hfa price
valacyclovir valtrex
lipinpril
ventolin
auto insurance
http://cialiscials.com/# - buy generic cialis online viagra cost buy real cialis online
lisinopril 10mg tablets
auto insurance quotes comparison
ventolin
ventolin inhaler no prescription
best auto insurance rates
trazodone
auto insurance
auto insurance
acyclovir
auto insurance quotes
dairyland insurance
ventolin medication
auto insurance
list of car insurance companies
propranolol 10mg
strattera generic
buy propranolol online
car insurance quotes how much is car insurance
everest auto insurance progressive auto insurance
301 Moved Permanently
301 Moved Permanently!..
Full of salient points. Don't stop beevniilg or writing!