无扩展 URL 尾部斜杠重定向 [英] Extensionless URL trailing slash redirect

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

问题描述

我已经在 Dreamhost 上托管的多个域上测试了以下代码,并且可以正常工作——除了我在去年添加的较新域.

I've tested the code below on several domains hosted on Dreamhost and it works—except on newer domains I've added within the last year.

RewriteEngine on

#unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ $1 [R=301,L]

#redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.*).php([#?][^ ]*)? HTTP/
RewriteRule ^(.*).php$ $1 [R=301,L]

#resolve .php file for extensionless php urls
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [L]

有效的例子:

ryanvanetten.com/resources/lame/ryanvanetten.com/resources/lame.php 都重定向到 ryanvanetten.com/resources/lame提供的实际文件是 lame.php

ryanvanetten.com/resources/lame/ and ryanvanetten.com/resources/lame.php both redirect to ryanvanetten.com/resources/lame and the actual file that is served is lame.php

破碎的例子:

airve.com/test/file 正确地提供 file.php 但重定向 airve.com/test/file/airve.com/test/file.php 不起作用.尝试它们,您会看到它们似乎在吐出内部绝对路径.当我独立尝试每个重定向以及尝试下面的基本重定向时,发生了同样的事情.

airve.com/test/file properly serves file.php but the redirects airve.com/test/file/ and airve.com/test/file.php don't work. Try them and you'll see they seem to be spitting out the internal absolute path. The same thing occurred when I tried each of the redirects independently and when I tried the basic redirect below.

Redirect 301 /test/file/ /test/file

.htaccess 中没有其他内容.知道问题是什么吗?

There is nothing else in .htaccess. Any idea on what the issue is?

推荐答案

在这两种情况下,您都重定向到 $1,并且您没有 RewriteBase 指令来告诉 Apache 在哪里 root 这样的相对路径.

You are redirecting to $1 in both cases, and you don't have a RewriteBase directive to tell Apache where to root such a relative path.

尝试在 $1 之前添加根斜杠以将其锚定到您的网络服务器的根目录,例如:

Try adding a root slash before the $1 to anchor it to the root of your webserver, for instance:

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

这篇关于无扩展 URL 尾部斜杠重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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