扩展名的URL斜杠重定向斜杠、扩展名、重定向、URL

2023-09-02 11:25:28 作者:暗夜霸主

我测试过的code以下的托管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指令,告诉阿帕奇哪里根这样的相对路径。

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把一根斜线把它固定到Web服务器的根目录下,例如:

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

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