doushanlv5184 2010-01-23 10:28
浏览 35
已采纳

使用mod_rewrite或像PHP这样的语言处理友好/干净/漂亮的URL是否更好?

I'm developing my first decent-sized PHP site, and I'm a bit confused about what the "right way" (assuming there ever is such a thing) to handle clean/friendly/pretty URLs in the application.

The way I see it, there are two main options (I'll use a simplified social news site as an example):

1. Use mod_rewrite to handle all potential URLs. This would look similar, but not identical, to the following:

RewriteRule ^article/?([^/]*)/?([^/]*)/?([^/]*) /content/articles.php?articleid=$1&slug=$2
RewriteRule ^users/?([^/]*)/?([^/]*) /content/users.php?userid=$1&username=$2
RewriteRule ^search/?([^/]*)/? /content/search.php?query=$1

2. Pass everything to some handler script and let it worry about the details:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) handler.php?content=$1

Clearly this is all untested "air code," but you get the point.

  • Is one of these two ways going to be seriously slower than the other? Presumably the mod_rewrite is slower, since I'll be forced to use .htaccess files for this.
  • Are there serious disadvantages to either of these approaches?
  • Is there a "best practice" for this sort of thing, or is it something that each developer tends to decide for themselves? I know WordPress uses option two (though it was more trouble than it was worth when I investigated exactly how they did it).
  • 写回答

5条回答 默认 最新

  • dtgta48604 2010-01-23 10:36
    关注

    Option 1 (.htaccess and several .php files) was often used "in the past" ; now, I see option 2 (every request going through one .php file) used a lot more.

    The main advantages I see with option 2 are :

    • you can add / modify any kind of URL without having to change any physical file like .htaccess
      • which means the format of the URLs can be configured in the admin section of your application, for example
    • you only have one entry point to your PHP code.
      • which means everything goes though index.php : if you need some code executed for all requests, put it there, and you're sure it'll always be executed.
      • That's used a lot with MVC frameworks, for instance.


    A couple of years ago, I would have gone with option 1 ; now that I use MVC and Frameworks, I always go with option 2.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 统计大规模图中的完全子图问题
  • ¥15 使用LM2596制作降压电路,一个能运行,一个不能
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 经gamit解算的cors站数据再经globk网平差得到的坐标做形变分析
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式