具有任意数量的“子目录”的URL美化
我正在开发我的第一个网站,该网站位于 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_rewrite
的 RewriteRule
和 RewriteCond 但我不确定在可维护性方面使用哪种方法。例如,如果我决定向
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将所有 URL 转发到控制器脚本并在 PHP 中进行路由要容易得多:
通过
my_script.php
定向对不存在的文件的所有请求。从那里,您可以检查请求 URI,将其分解为由正斜杠分隔的段,然后路由到正确的文件。It's far easier to forward all URLs to your controller script, and do your routing in PHP:
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.