问题
I need some help, It´s my first time using the Rewrite Rule for a link.
My website is a eshop.
The url will be something like this:
www.mydomain.com/shop.php
This file, shop.php will receive up to 3 parameters throught the url.
Example:
Case 1: www.mydomain.com/shop.php?page=1
Case 2: www.mydomain.com/shop.php?page=1&category=1
Case 3: www.mydomain.com/shop.php?page=1&category=1&subcategory=3
What I need is that the Rewrite Rule returns me something like:
Case 1: www.mydomain.com/shop.html/1
Case 2: www.mydomain.com/shop.html/1/FeaturesItems/
Case 3: www.mydomain.com/shop.html/1/FeaturesItems/BrandName
Let's Imagine that FeaturesItems is the Category that his Id is the number 1.
Let's Imagine that BrandName is the SubCategory that his Id is the number 3.
Can you help me to create the rewrite rule?
The other question is, in shop.php I can read the $_GET['category'] with the Id?
回答1:
I think you want to acomplish a call to /shop/15
and redirect this internaly to shop.php?category=15
. Then do something like this:
RewriteRule ^shop.html/(\d+)$ shop.php?category=$1 [L]
RewriteRule ^shop.html/(\d+)/FeaturedItems/ shop.php?category=$1&featured=1 [L]
and so on.
(untested)
Maybe get a more indepth look of how mod_rewrite works. For example:
http://code.tutsplus.com/tutorials/an-in-depth-guide-to-mod_rewrite-for-apache--net-6708 https://www.digitalocean.com/community/tutorials/how-to-set-up-mod_rewrite http://www.workingwith.me.uk/articles/scripting/mod_rewrite
I could also recommend the excelent mod_rewrite-Cheat-Sheet. Also check the tutorial section there.
来源:https://stackoverflow.com/questions/33525892/rewrite-rule-help-first-time