dongrong7267 2009-05-01 18:23
浏览 42

如何在PHP中创建友好的URL?

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 is the user id.

In recent years, I found some website with very nice urls like:

www.domain.com/profile/12345

How do I do this in PHP?

Just as a wild guess, is it something to do with the .htaccess file? Can you give me more tips or some sample code on how to write the .htaccess file?

  • 写回答

8条回答 默认 最新

  • duanpo1821 2009-05-01 18:26
    关注

    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'
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大