mod_rewrite:添加尾部斜杠?

发布于 2024-09-07 11:59:31 字数 1126 浏览 3 评论 0原文

我正在使用 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一刻暧昧 2024-09-14 11:59:31

这一行:

RewriteCond %{REQUEST_FILENAME} -D

应该是:

RewriteCond %{REQUEST_FILENAME} -d

This line:

RewriteCond %{REQUEST_FILENAME} -D

Should have been:

RewriteCond %{REQUEST_FILENAME} -d
朮生 2024-09-14 11:59:31

不要为 Mod Rewrite 烦恼,有一个指令

<Location /some/path>
  DirectorySlash On
  SetHandler some-handler
</Location>

http://httpd .apache.org/docs/2.2/mod/mod_dir.html

Don't bother with Mod Rewrite, there is a directive for it

<Location /some/path>
  DirectorySlash On
  SetHandler some-handler
</Location>

http://httpd.apache.org/docs/2.2/mod/mod_dir.html

年华零落成诗 2024-09-14 11:59:31

你的意思是

RewriteRule ^(.*)$ $1/ [L]

相反吗?

我不知道你的做法。我会尝试类似

RewriteRule ^/(\S+)/?$ /myphp.php?url=$1

我之前用它来重写的方法,并且它有效:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain\.com
RewriteRule ^(.*)$ http://www.domain.com$1
RewriteRule ^/(\S+)/?$ /myphp.php?url=$1

请注意,我没有在第一条规则中包含尾随斜杠。

另外,我建议您不要依赖 .htaccess ,除非绝对必要。这是因为服务器会不断检查 .htaccess 文件。使用 httpd.conf 文件意味着 apache 只会加载一次条件和规则。至少我被建议这样做。

Did you mean

RewriteRule ^(.*)$ $1/ [L]

instead?

I don't know about your approach. I would try something like

RewriteRule ^/(\S+)/?$ /myphp.php?url=$1

I've used this for rewriting before and it works:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain\.com
RewriteRule ^(.*)$ http://www.domain.com$1
RewriteRule ^/(\S+)/?$ /myphp.php?url=$1

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.

不念旧人 2024-09-14 11:59:31

试试这个。它将向没有尾部斜杠的目录请求添加尾部斜杠。

<IfModule mod_rewrite.c>
RewriteEngine On

#use the directory viewer on directories without an index page
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*?)/?$ /myphp.php?url=$1/ [QSA,L]  

</IfModule>

Try this. It will add trailing slash to directory requests which don't have trailing slash.

<IfModule mod_rewrite.c>
RewriteEngine On

#use the directory viewer on directories without an index page
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*?)/?$ /myphp.php?url=$1/ [QSA,L]  

</IfModule>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文