【问题标题】:Please help me with URL routing [closed]请帮助我进行 URL 路由 [关闭]
【发布时间】:2013-01-15 14:06:58
【问题描述】:

我是这个路由和 mod-reqrite 等的完整初学者。东西。 我在这里遵循了有关 URL 路由的教程:http://wettone.com/code/clean-urls,但无法使其正常工作。

我只想路由 ALL 和任何类型的 URL,例如

http://wettone.com/weblog/2000/01/01/example
http://wettone.com/weblog/2000/01/01
http://wettone.com/weblog/2000/01
http://wettone.com/weblog/2000

到我在文档根目录的 weblog 文件夹中的 index.php 脚本,目录路径为:

http://wettone.com/weblog/index.php?y=2000&m=01&d=01&n=example
http://wettone.com/weblog/index.php?y=2000&m=01&d=01
http://wettone.com/weblog/index.php?y=2000&m=01
http://wettone.com/weblog/index.php?y=2000

我该怎么做。 顺便说一句,我更喜欢保持简单,不使用任何框架

提前谢谢...

编辑:我可以知道为什么要投反对票吗?我做错了什么?我不能在这里问这种类型的问题吗?

【问题讨论】:

  • 没有人能猜到您为什么会收到内部服务器错误。网络服务器日志会告诉你。这也超级简单,请阅读更多教程;考虑写一个不那么无意义的标题来找到一些。
  • 查看ToroPHP。本周我偶然发现了超小的“框架”。基本上只给你你所追求的,而不是更多:路由/干净的网址:)
  • ToroPHP 正是我想要的。非常感谢你:-)

标签: php mod-rewrite


【解决方案1】:

使用 htaccess

Options +FollowSymLinks
RewriteEngine On
RewriteBase /weblog/


RewriteRule (.*)/(.*)/(.*)/(.*)$ index.php?y=$1&m=$2&d=$3&n=$4

【讨论】:

  • 它起作用了。给出内部服务器错误。我的 index.php 位于 weblog 文件夹中,并且我已将 htaccess 放在文档根目录中。对吗?
  • @NitinBansal : 已编辑代码请立即查看
  • .htaccess 应该与 index.php 在同一目录中,不是吗?我相信RewriteBase 应该设置为“webroot”。 (/ 如果托管在 example.com/foo/ 如果托管在 example.com/foo 等。
【解决方案2】:

所以基本上您希望所有内容都映射到 index.php? 如果是,这里的 sn-p 取自 symfony1 htaccess

# we check if the .html version is here (caching)
RewriteRule ^$ index.html [QSA]
RewriteRule ^([^.]+)$ $1.html [QSA]
RewriteCond %{REQUEST_FILENAME} !-f

# no, so we redirect to our front web controller
RewriteRule ^(.*)$ index.php [QSA,L]

如果我没记错的话,你必须使用 $_SERVER['REQUEST_URI'] 来解析 uri 并决定显示什么

最好只使用 symfony2 组件来完成这项工作http://symfony.com/doc/current/components/routing/introduction.html :)

【讨论】:

  • Thnx...我试过但它给了我“内部服务器错误”错误。怎么了?顺便说一句,我已将此 htaccess 放在我的文档根目录中,并且 index.php 位于 weblog 文件夹中。对吗?
  • 你有没有打开mod_rewrite,然后你重启了apache?
  • 好的,mod_rewrite 被评论了,但我取消了它并重新启动了 apache。让我再列举几个排列/组合
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-05
  • 1970-01-01
  • 2016-10-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多