无扩展 URL 尾部斜杠重定向

编程入门 行业动态 更新时间:2024-10-26 06:34:08
本文介绍了无扩展 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/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 尾部斜杠重定向

本文发布于:2023-11-01 11:26:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1549214.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:斜杠   尾部   重定向   URL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!