mod_rewrite:添加尾部斜杠?
我正在使用 .htaccess 文件将对目录的请求定向到自定义 php 文件,该文件提供有关该目录的信息(并且我希望浏览器显示的 url 不更改)。
这是 .htaccess 文件的相关部分。
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ /myphp.php?url=%{REQUEST_URI} [QSA,L]
如果您转到目录并包含尾部斜杠,则效果很好:
http://domain.com/path/
但如果没有尾部斜杠,则不会
http://domain.com/path
url(在浏览器中显示)会变成:
http://localhost:8888/path/?url=/path
我尝试通过在此规则之上添加一条规则来解决此问题:
RewriteCond %{REQUEST_FILENAME} -D
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ $1/ [L]
但这对我不起作用。
如果省略了尾部斜杠,如何让 .htaccess 添加尾部斜杠,然后就像它已经存在一样?
谢谢
更新: 根据要求,这里是全部内容。
<IfModule mod_rewrite.c>
RewriteEngine On
#force trailing slashes on real directories
RewriteCond %{REQUEST_FILENAME} -D
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ $1/ [L]
#use the directory viewer on directories without an index page
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ /myphp.php?url=%{REQUEST_URI} [QSA,L]
</IfModule>
I am using a .htaccess file to direct requests for a directory to a custom php file that provides info about that directory (and i want the url displayed by the browser to not change).
Here is the relevant part of the .htaccess file.
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ /myphp.php?url=%{REQUEST_URI} [QSA,L]
This works well if you go to a directory and include a trailing slash:
http://domain.com/path/
But without the trailing slash, it doesn't
http://domain.com/path
The url (shown in the browser) turns into:
http://localhost:8888/path/?url=/path
I've tried fixing this by adding a rule above this rule:
RewriteCond %{REQUEST_FILENAME} -D
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ $1/ [L]
But that isn't working for me.
How can I get .htaccess to add the trailing slash if it is omitted and then act just as if it had been there?
Thank you
Update:
As requested, here is the whole thing.
<IfModule mod_rewrite.c>
RewriteEngine On
#force trailing slashes on real directories
RewriteCond %{REQUEST_FILENAME} -D
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ $1/ [L]
#use the directory viewer on directories without an index page
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ /myphp.php?url=%{REQUEST_URI} [QSA,L]
</IfModule>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这一行:
应该是:
This line:
Should have been:
不要为 Mod Rewrite 烦恼,有一个指令
http://httpd .apache.org/docs/2.2/mod/mod_dir.html
Don't bother with Mod Rewrite, there is a directive for it
http://httpd.apache.org/docs/2.2/mod/mod_dir.html
你的意思是
相反吗?
我不知道你的做法。我会尝试类似
我之前用它来重写的方法,并且它有效:
请注意,我没有在第一条规则中包含尾随斜杠。
另外,我建议您不要依赖 .htaccess ,除非绝对必要。这是因为服务器会不断检查 .htaccess 文件。使用 httpd.conf 文件意味着 apache 只会加载一次条件和规则。至少我被建议这样做。
Did you mean
instead?
I don't know about your approach. I would try something like
I've used this for rewriting before and it works:
Note that I didn't include the trailing slash in the first rule.
Also, I would advice you not to rely on .htaccess unless absolutely necessary. This is because the server will check for .htaccess files constantly. Using the httpd.conf file instead means apache will only load conditions and rules once. At least I was adviced to do so because of this.
试试这个。它将向没有尾部斜杠的目录请求添加尾部斜杠。
Try this. It will add trailing slash to directory requests which don't have trailing slash.