在根目录和非根目录中使用 PHP 进行动态 URL 重写
对于我的程序,我使用 PHP 进行动态 URL 重写:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
#Rewrite the URI if there is no file or folder
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#rewrite all to index.php
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
然后在 PHP 中,我将 URL 分解为不同的部分。例如http://myhost/foo/bar
返回foo bar
。我遇到的问题是,如果程序不在服务器的根目录中,例如 http://myhost/this/is/a/folder/hereistheprogram/foo/bar ,因为那么脚本返回这是一个文件夹hereistheprogram foo bar
。现在遇到的问题是我无法区分文件夹和 URL 参数。
For my program I use dynamic URL rewriting with PHP:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
#Rewrite the URI if there is no file or folder
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#rewrite all to index.php
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
In PHP I then break down the URL in its different parts. For example http://myhost/foo/bar
returns foo bar
. The problem I have is if the program is not located in the root directory of the server, for example http://myhost/this/is/a/folder/hereistheprogram/foo/bar
, because then the script returnes this is a folder hereistheprogram foo bar
. Now have the problem that I can't differentiate between the folders and the URL parameters.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要知道脚本文件的路径是什么。您可以通过显式声明路径前缀或自动确定它来做到这一点,例如:
然后您可以使用该前缀并将其从请求 URI 路径中删除:
顺便说一下: 如果您不传递路径前缀,那就更好了显式向您的 index.php 请求 URI 路径,但从
$_SERVER['REQUEST_URI']
检索它:以及相应的 mod_rewrite 规则:
You need to know what the path to the script file is. You can do that by either explicitly declaring the path prefix or by determining it automatically, for example:
Then you can use that prefix and strip it from the request URI path:
By the way: It would be better if you don’t pass the request URI path explicitly to your index.php but retrieve it from
$_SERVER['REQUEST_URI']
:And the corresponding mod_rewrite rule:
不太确定确切的问题,但是..
您能否在网址中使用常量“foo”标识符来识别在何处中断字符串?
Not too sure of the exact problem but..
Could you have constant 'foo' identifier used in the urls to identify where to break the string?