.htaccess url rewriting with php

前端 未结 2 1605
春和景丽
春和景丽 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条回答
  • 2021-01-26 10:36

    For better search search engine optimisation have URLs like stackoverflow -

    http://www.bentinckfencing.co.uk/brochure/24/concrete-fence-posts
    

    There is something like search engines do not store whatever is after the ? in your URLs. This ending concrete-fence-posts also helps users to identify the page in the browser address bar. But, you cannot allow certain special characters if the ? is not used in the URL. Characters like /, % will break the URL and \, #, + will not appear.

    Updates

    Do you want to move completely from ID passing to name passing? I am not sure about how good that will be, but I have heard that a few well known sites have URLs like that. But, you may have to do many changes depending on your project (how many modules you have). The stackoverflow style URL will, however, retain the ID based item fetching.

    Further Updates
    You can try this rewrite rule to have both ID and name in the URL (stackoverflow style):-

    RewriteRule ^([a-z0-9\-\_]+)/([0-9]+)/([a-z0-9\-\_]+)/?$ $1.php?cat_path=$1&cat_name=$2
    

    I have not tested it though.

    Another important thing, I see that your code enters brochure.php directly and there is no common file (common controller) which is executed in case of all the modules. This approach can have many drawbacks, particularly if your's is a medium to large application. It is always better to execute a common index.php file and include the needed brochure.php file there so that you can perform all those common logics (needed in all your modules) in this index.php file. I have a common index.php file and the following rewrite rules do the same:-

    ############To remove display of "index.php" from URL
    RewriteCond $1 !^(index\.php|resources|robots\.txt)
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    
    RewriteRule ^(.*)$ index.php [L,QSA]
    #################################################end of hiding index.php
    
    RewriteRule ^([a-z0-9\-\_]+)/([0-9]+)/([a-zA-Z0-9\-\_]+)/?$ index.php?a=$1&b=$2&c=$3
    

    Note:- The first block of rewrite rules hides the index.php from the URL and a visitor does not know that my application runs in PHP.

    Finally I have URLs like this:-

    http://www.bentinckfencing.co.uk/brochure/24/concrete-fence-posts
    

    Hope this helps,
    Sandeepan

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题