.htaccess url rewriting with php

前端 未结 2 1603
春和景丽
春和景丽 2021-01-26 10:22

I currently have an entire website running on PHP & GET variables.

My links look like this at present

 http://www.example.co.uk/brochure.php?cat_path         


        
2条回答
  •  闹比i
    闹比i (楼主)
    2021-01-26 10:48

    Assuming your parameters are always named cat_path and product_id (if it exists) you can do something like this:

    RewriteRule ^([^/]+)/([\d+])$         $1.php?cat_path=$2
    RewriteRule ^([^/]+)/([\d+])/([\d+])$ $1.php?cat_path=$2&product_id=$3
    

    Your URLs would then be in one of these formats:

    pagename/cat_path
    pagename/cat_path/product_id
    

    For example:

    http://www.bentinckfencing.co.uk/brochure/24
    http://www.bentinckfencing.co.uk/product/35/54
    

    Edit: I see you want to use product names in the URL. In that case, your PHP scripts will need to be able to take a name as a parameter and look up the ID. You should continue to accept the ID directly so as to not break existing links. Then your rewrite rule would look like this:

    RewriteRule ^([^.]+)$         brochure.php?name=$1
    

    And http://www.bentinckfencing.co.uk/Concrete_Fence_Posts would rewrite to http://www.bentinckfencing.co.uk/brochure.php?name=Concrete_Fence_Posts.

提交回复
热议问题