如何使用mod_rewrite从.htaccess引用当前目录? [英] How to reference the current directory from .htaccess using mod_rewrite?

查看:48
本文介绍了如何使用mod_rewrite从.htaccess引用当前目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用mod_rewrite创建漂亮的URL,但是有一个.htaccess文件的单一版本,可用于服务器上的任何用户.

I'd like to use mod_rewrite to make pretty URLs, but have a single version of the .htaccess file that can be used for any user on a server.

到目前为止,我有标准的漂亮网址.htaccess:

So far I have the standard pretty URL .htaccess:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$     /index.php/$1 [L]

理想情况下,我想要类似

Ideally, I would like something similar to

RewriteRule ^(.*)$     %{URL of this file's directory}/index.php/$1 [L]

此文档会让我相信我想要的东西不是必需的:

This documentation would lead me to believe that what I want is not necessary:

注意:每个目录上下文中的模式匹配 永远不要忘记,模式已应用于每个服务器配置文件中的完整URL.但是,在每个目录的配置文件中,将自动删除每个目录的前缀(对于特定目录始终是相同的)以进行模式匹配,并在替换完成后自动添加.此功能对于许多类型的重写都是必不可少的-没有此重写,您将始终必须匹配并非总是可能的父目录.

Note: Pattern matching in per-directory context Never forget that Pattern is applied to a complete URL in per-server configuration files. However, in per-directory configuration files, the per-directory prefix (which always is the same for a specific directory) is automatically removed for the pattern matching and automatically added after the substitution has been done. This feature is essential for many sorts of rewriting - without this, you would always have to match the parent directory which is not always possible.

但是,无论是否在RewriteRule上的index.php前面加一个前导/,我仍然得到错误的结果.

I still get the wrong result, though, whether or not I put a leading / in front of the index.php on the RewriteRule.

这个

http://server/test/stream/stream

变成

http://server/index.php/stream

不是

http://server/test/index.php/stream

.htaccess文件位于/test/中时.

when the .htaccess file is in /test/.

推荐答案

事实证明,是否在index.php前面加一个前导/并不重要.通过关闭它,脚本可以正常工作.我一直在测试R标志,该标志在重定向上使用物理目录.

Turns out that it does matter whether I put a leading / in front of index.php or not. By leaving it off, the script works correctly. I had been testing with the R flag which was using physical directories on the redirect.

这篇关于如何使用mod_rewrite从.htaccess引用当前目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆