无需 mod_rewrite 即可清理 PHP 中的 URL
我正在为一个用 PHP 为客户端构建的网站(我很少使用它)开发一个后端,该网站即将完成。我主要是一名 Rails 开发人员,所以我很难不选择一些混蛋版本的 CRUD 设置,并且我正在尝试清理 url。
我希望我的网址如下所示:
http://www.example.com/ video/name-of-video/
我计划只从 $_SERVER['PHP_SELF']
中获取网址的最后一位,并使用它搜索数据库,但我会做什么喜欢做的是将index.php文件扔进http://www.example.com/videos/ 目录并在我明确指向时使用它有人http://www.example.com/videos/name-of-video/ ,如果这是有道理的话。
如果我将 index.php 放在 /videos/ 目录中,这会按原样工作吗?还是需要其他东西?是否可以不搞乱 htaccess 呢?
抱歉我的解释很可悲。我希望这是有道理的。
谢谢!
I am developing a backend for a site I built for a client in PHP (which I rarely use), which is almost completed. I'm a Rails developer primarily, so I'm having a hard time not going for some bastardized version of a CRUD setup, and I am attempting to clean up the urls.
I would like my urls to be like so:
http://www.example.com/videos/name-of-video/
I plan on just taking the last bit of the url from $_SERVER['PHP_SELF']
and searching through the DB using that, but what I would like to do is throw the index.php file in the http://www.example.com/videos/ directory and have it be used when I explicitly point someone to http://www.example.com/videos/name-of-video/, if that makes sense.
Will this work as is, if I put the index.php in the /videos/ directory, or is something else needed? Is it possible to do without messing around with htaccess?
Sorry for my pathetic explanation. I hope it makes sense.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有几个选项可以在没有 RewriteRule 的情况下完成此操作。并非没有 .htaccess 混乱。
首先,您可以命名一个不带扩展名的 PHP 脚本,即
video
而不是video.php
,然后通过ForceType
或设置实际文件类型默认类型
。所以 PHP 解释器仍然会接受它。该路径将显示在$_SERVER['PATH_INFO'
中,同样,启用的
+MultiViews
允许 PHP 脚本不具有扩展名。最后,可以使用
ErrorDocument
处理程序代替真正的 RewriteRule。然而它也带来了许多缺点。index.php
本身不会在虚假路径中获取任何随机文件名。唯一可行的选择是使用像http://www.example.com/videos/?name-of-video/
这样的 URL,所以不行。如果没有一些配置,就没有漂亮的目录模仿URL。
There are a few options to do it without a RewriteRule. Not without .htaccess messing.
First you can name a PHP script without extension, that is
video
instead ofvideo.php
, and then set the actual file type viaForceType
orDefaultType
. So the PHP interpreter will still pick it up. The path will show up in$_SERVER['PATH_INFO'
Likewise would an enabled
+MultiViews
allow a PHP script not to have an extension.And lastly an
ErrorDocument
handler can be used in place of a real RewriteRule. It brings any number of drawbacks with it however.An
index.php
by itself will not pick up any random filenames in a fake path. The only option for that to work is using URLs likehttp://www.example.com/videos/?name-of-video/
So no. No nice URLs with directory mimicking without some configuration.
据我所知,如果不接触 .htaccess,这种操作是不可能的。
当您编写网址 http://www.example.com/videos/name-of -video/ 你的服务器肯定会在文件夹 name-of-video 文件夹中查找 index.php 文件。这是不存在的。
如果这个任务可以在没有 .htaccess 的情况下完成,那么它们就不会被使用。
顺便问一下,你为什么不想使用 .htaccess..?
As per my knowledge this manipulation is not possible without touching .htaccess.
when you write the url http://www.example.com/videos/name-of-video/ your server is definitely going to look for the index.php file in the folder name-of-video folder. which does not exist.
If this task could have been achieved without .htaccess then they would have never been used.
And by the way why don't you want to use .htaccess..??