url rewriting in php

ぐ巨炮叔叔 提交于 2019-12-08 05:13:24

问题


Hai

I have one more doubt in apache mod_rewrite. I want to rewrite the url mydomain/index.php?category=1&id=1 To mydomain/index/category/1/id/1 How I write rule in .htaccess

And what is the link that i have to give inside the a tag

Please give me a solution..


回答1:


Not tested, but worth a shot:

RewriteEngine On
RewriteRule ^index/category/([0-9]+)/id/([0-9]+)$ index.php?category=$1&id=$2

Your URLs can look exactly like the way you mentioned:

Category 1
<a href="index/category/1/id/1">Product 1</a>
<a href="index/category/1/id/2">Product 2</a>
Category 2
<a href="index/category/2/id/3">Product 3</a>
<a href="index/category/2/id/4">Product 4</a>



回答2:


Inside the <a> tags you will use the nice link, that is category/1/id/1 (that's exactly why you're using mod_rewrite, to be able to use nice URLs!)

As for the rule, try something like (untested):

RewriteRule category/(.*)/id/(.*)$ index.php?category=$1&id=$2&%{QUERY_STRING} [L]

Actually I would rather use

RewriteRule (.*)/(.*)$ index.php?category=$1&id=$2&%{QUERY_STRING} [L]

So you can call directly mydomain/1/1 but you got the idea I hope

EDIT: the &%{QUERY_STRING} part is not necessary for what you asked, but I generally include it in case I want to pass any extra parameter to the page.



来源:https://stackoverflow.com/questions/2896015/url-rewriting-in-php

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