url rewriting in php

2019-06-03 08:50发布

问题:

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.