网址重写 mod_rewrite [英] Url rewriting mod_rewrite
问题描述
我有这样的网址:
page.com/content.php?xname=p&yname=q&zid=1
我想使用 apache mod_rewrite 将这个 url 改写成类似的东西:
I want to rewrite this url using apache mod_rewrite into something like:
page.com/p/q/
注意重命名的 url 中不应有zid"参数.我知道表达式作为 GET 传递到原始 url 中.是否可以重命名如上.如果是,如何实现?
note there should not be 'zid' parameter in renamed url. I know expressions are passed as GET into the original url. Is it possible to rename as above. If yes, How to achieve this?
推荐答案
这个对我来说很好用,并且会将 /p/q/
的请求重写为 /content.php?xname=p&yname=q&zid=1
.
This one works fine for me and will rewrite request for /p/q/
to /content.php?xname=p&yname=q&zid=1
.
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/$ content.php?xname=$1&yname=$2&zid=1 [QSA,L]
这个规则要放在网站根目录下的.htaccess中.如果放在其他地方,可能需要进行一些小的调整.
This rule is to be placed in .htaccess in website root folder. If placed elsewhere some small tweaking may be required.
如果请求的 URL 是真实的文件或文件夹,它不会重写(我确定你不想重写图像或其他一些页面——我不得不添加这样的条件,因为我不知道什么是您的网站结构是).
It will not rewrite if requested URL is a real file or folder (I'm sure you do not want to rewrite images or some other pages -- I had to add such condition since I do not know what is your website structure is).
这篇关于网址重写 mod_rewrite的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!