我已经在 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/resources/lame/ 和 ryanvanetten/resources/lame.php 都重定向到 ryanvanetten/resources/lame提供的实际文件是 lame.php
ryanvanetten/resources/lame/ and ryanvanetten/resources/lame.php both redirect to ryanvanetten/resources/lame and the actual file that is served is lame.php
破碎的例子:
airve/test/file 正确地提供 file.php 但重定向 airve/test/file/ 和 airve/test/file.php 不起作用.尝试它们,您会看到它们似乎在吐出内部绝对路径.当我独立尝试每个重定向以及尝试下面的基本重定向时,发生了同样的事情.
airve/test/file properly serves file.php but the redirects airve/test/file/ and airve/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 尾部斜杠重定向
发布评论