具有任意数量的“子目录”的URL美化

发布于 2024-10-10 01:50:43 字数 973 浏览 5 评论 0原文

我正在开发我的第一个网站,该网站位于 PHP/Apache 堆栈之上。

我的网站有一个结构如下的逻辑菜单:

+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

我的目标是拥有如下所示的有效 URL:

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

我已阅读有关 mod_rewriteRewriteRuleRewriteCond 但我不确定在可维护性方面使用哪种方法。例如,如果我决定向 AACAA 添加另一个级别怎么办?我是否需要一遍又一遍地搞乱mod_rewrite

将所有内容重定向到 index.php 并手动解析 REQUEST_URI 是否更合适? 专业人士是如何做到这一点的?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁月如刀 2024-10-17 01:50:43

将所有 URL 转发到控制器脚本并在 PHP 中进行路由要容易得多:

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

通过 my_script.php 定向对不存在的文件的所有请求。从那里,您可以检查请求 URI,将其分解为由正斜杠分隔的段,然后路由到正确的文件。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文