Url rewriting mod_rewrite

旧城冷巷雨未停 提交于 2020-01-09 11:51:48

问题


I have the url such as:

 page.com/content.php?xname=p&yname=q&zid=1

I want to rewrite this url using apache mod_rewrite into something like:

 page.com/p/q/

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?


回答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]
  1. This rule is to be placed in .htaccess in website root folder. If placed elsewhere some small tweaking may be required.

  2. 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).




回答2:


RewriteRule   ^content\.php\?xname=(p)&yname=(q)&zid=1$    /$1/$2  [R]

Instead of p and q you can try expressions like [a-Z0-9_-]+ to match identifiers.

There's an online testing tool here: http://civilolydnad.se/projects/rewriterule/




回答3:


RewriteEngine On
RewriteRule ^/[a-z0-9]+/[a-z0-9]+/$ content.php?xname=$1&yname=$2 [L]


来源:https://stackoverflow.com/questions/7211259/url-rewriting-mod-rewrite

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!