mod_rewrite:添加尾部斜杠? [英] mod_rewrite: add trailing slash?

查看:30
本文介绍了mod_rewrite:添加尾部斜杠?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 .htaccess 文件将目录请求定向到提供有关该目录的信息的自定义 php 文件(并且我希望浏览器显示的 url 不会更改).

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).

这是 .htaccess 文件的相关部分.

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]

但这对我不起作用.

我怎样才能让 .htaccess 添加尾部斜杠,如果它被省略,然后就好像它已经在那里一样?

How can I get .htaccess to add the trailing slash if it is omitted and then act just as if it had been there?

谢谢

更新:按照要求,这里是整件事.

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>

推荐答案

这一行:

RewriteCond %{REQUEST_FILENAME} -D

应该是:

RewriteCond %{REQUEST_FILENAME} -d

这篇关于mod_rewrite:添加尾部斜杠?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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