duanlie2709 2011-01-04 23:21
浏览 39
已采纳

具有任意数量的“子目录”的URL预处理

I'm working on my first site which lives on top of a PHP/Apache stack.

My site has a logical menu structured like this:

+AAA Entry
  -AAB Entry
    -AABA Subentry
    -AABB Subentry
  -AAC Entry
    -AACA Subentry
      -AACAA Subentry
      -AACAB Subentry
    -AACB Subentry
  -AAD Entry

+BBB Entry
  -BBC Entry
    -BBCA Subentry
    etc.
+CCC Entry
+DDD Entry

My goal is to have valid URLs like these:

http://www.mydomain.com/aaa/aab/aaba
http://www.mydomain.com/aaa/aac/aaca/aacaa
http://www.mydomain.com/aaa/aac/aacb
http://www.mydomain.com/aaa/aad

http://www.mydomain.com/bbb
http://www.mydomain.com/bbb/bbc
http://www.mydomain.com/bbb/bbc/bbca

http://www.mydomain.com/ccc

I've read about mod_rewrite's RewriteRule and RewriteCond but I'm unsure as to which method to use in terms of maintainability. What if I decide to add another level to AACAA, for example? Will I have to mess with mod_rewrite over and over again?

Is it more appropriate to redirect everything to index.php and parse REQUEST_URI manually? How is this done by professionals?

  • 写回答

1条回答 默认 最新

  • douan3414 2011-01-04 23:27
    关注

    It's far easier to forward all URLs to your controller script, and do your routing in PHP:

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule .* my_script.php [L]
    

    The directs all requests for files which do not exist through my_script.php. From there, you can examine the request URI, explode it into segments delimited by forward slashes, and route to the correct file.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效