.htaccess url rewriting with php

前端 未结 2 1632
春和景丽
春和景丽 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

提交回复
热议问题