How to create friendly URL in php?

后端 未结 8 1839
一整个雨季
一整个雨季 2020-11-21 11:06

Normally, the practice or very old way of displaying some profile page is like this:

www.domain.com/profile.php?u=12345

where u=12345

相关标签:
8条回答
  • 2020-11-21 12:02

    According to this article, you want a mod_rewrite (placed in an .htaccess file) rule that looks something like this:

    RewriteEngine on
    RewriteRule ^/news/([0-9]+)\.html /news.php?news_id=$1
    

    And this maps requests from

    /news.php?news_id=63
    

    to

    /news/63.html
    

    Another possibility is doing it with forcetype, which forces anything down a particular path to use php to eval the content. So, in your .htaccess file, put the following:

    <Files news>
        ForceType application/x-httpd-php
    </Files>
    

    And then the index.php can take action based on the $_SERVER['PATH_INFO'] variable:

    <?php
        echo $_SERVER['PATH_INFO'];
        // outputs '/63.html'
    ?>
    
    0 讨论(0)
  • 2020-11-21 12:03

    Simple way to do this. Try this code. Put code in your htaccess file:

    Options +FollowSymLinks
    
    RewriteEngine on
    
    RewriteRule profile/(.*)/ profile.php?u=$1
    
    RewriteRule profile/(.*) profile.php?u=$1   
    

    It will create this type pretty URL:

    http://www.domain.com/profile/12345/

    For more htaccess Pretty URL:http://www.webconfs.com/url-rewriting-tool.php

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