How can i create better urls using .htacces modrew

2019-08-25 18:56发布

问题:

This are my urls right nowm for my products

http://www.example.com/product.php?product=32723

I want to achieve this

http://www.example.com/product/32723-brand-model-productname

I have been modifying my .htaccess but really with no clue on how to achieve this.

回答1:

You must match your URL with a RewriteRule pattern and rewrite it to the target URL

RewriteRule ^/product/(\d+)- /product.php?product=$1

This pattern matches any URL starting with /product/ and captures the following digits (\d+) followed by a dash -. The substitution URL will be /product.php?product= with the captured digits $1 appended.

To capture some part of the match, you enclose it in parenthesis (...). Read more on regular expressions used in mod_rewrite at Apache mod_rewrite Introduction - Regular Expressions.



回答2:

This question is so common I just put up the whole answer with code here:

http://www.prescia.net/bb/coding/5-141018-simple_friendly-url