【发布时间】:2015-03-13 17:56:19
【问题描述】:
我的主机上安装了一个基于 php 的系统(称为 OJS)。它使用 SMARTY 来创建页面。我有带有 URL 的页面,例如: http://example.com/ojs/index.php/foo
我希望能够在地址栏中输入http://example.com/ojs/foo,.htaccess 会将 index.php 添加到 url 中,并执行新的完整 URL,以便 index.php 可以生成所需的页面。
现在我在 ojs 文件夹中的 .htaccess 文件中使用以下内容。
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/$1 [QSA,L]
</IfModule>
我还在 .htaccess 中尝试了许多其他语法和指令。但它们都不起作用。 当我在地址栏中输入http://example.com/ojs/index.php/foo 时,我得到了我想要的页面。 当我输入http://example.com/ojs/foo(或任何未在ojs 中定义的页面,例如http://example.com/ojs/blahbla 或http://example.com/ojs/index.php/blahblahbla)时,它会显示主页http://example.com/ojs/
我做了一个测试,我猜 Rewrite rule 至少在做一些事情。我在文件 index2.php(在 ojs 文件夹中)中创建了一个虚拟 php 代码,它与超全局值相呼应。对于这个测试,我修改了重写规则以包含 index2.php,我得到以下结果:
- 地址:http://example.com/ojs/(根据需要)没有进行 URL 重写,因为我没有看到 index2.php 的内容
- 地址:http://example.com/ojs/anything 我看到 index2.php 的内容与以下值相呼应:
$_SERVER['HTTP_HOST']: example.com $_SERVER['REQUEST_URI']: /ojs/anything $_SERVER[SCRIPT_NAME]: /ojs/index2.php $_SERVER['PHP_SELF']: /ojs/index2.php $_SERVER['REQUEST_METHOD']: GET $_SERVER['PATH_INFO']: $_SERVER['ORIG_PATH_INFO']: /anything __FILE__: /home/user/public_html/ojs/index2.php
谁能想到为什么它适用于 index2.php 而它不适用于 index.php ?
如果有帮助,我的配置:
PHP 版本 5.3.29, Apache 2.2.29 版, 主机:Cpanel,共享 Linux 主机,
我无权访问主错误日志或 Apache 配置文件。
非常感谢,
【问题讨论】:
标签: php apache .htaccess mod-rewrite url-rewriting