我301怎么可以重定向多个领先的斜杠(///file.txt)只是(/file.txt)? [英] How can I 301 redirect multiple leading slashes (///file.txt) to just (/file.txt)?

查看:216
本文介绍了我301怎么可以重定向多个领先的斜杠(///file.txt)只是(/file.txt)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的apache的conf设置为显示对像这样一个txt文件的PHP生成的文本:

I have my apache conf set to show php generated text on a .txt file like so:

Alias /file.txt /var/www/domain.com/html/file.php

问题是,现在这个适用于任何数量领先的斜线。例如,以下所有工作的:

The problem is that this now works for any number of leading slashes. For example, all of the following work:

domain.com/file.txt
domain.com//file.txt
domain.com///file.txt

不过,我想所有的多斜线重定向到根:

But I'd like all multiple slashes to redirect to the root:

domain.com//file.txt =====[301]=====> domain.com/file.txt

这通常不会是除了这些多重斜线的URL实际上是索引的一个问题!哎呀。

This normally wouldn't be an issue except one of these multiple slashes URLs was actually indexed! Ooops.

现在我需要301(永久)所有的多斜线重定向到根。 (所以//file.txt被重定向到/file.txt)

Now I need to 301 (permanent) redirect all multiple slashes to the root. (so //file.txt is redirected to /file.txt)

您能帮我吗?

推荐答案

当您在浏览器中键入: http://domain.com////////file.txt ,重写引擎只看到作为URI: /file.txt 。但是,如果你在浏览器中键入: http://domain.com///path////to/////file.txt ,重写引擎看到URI为 /路径////到///// file.txt的。额外的斜线开头从来没有在%{REQUEST_URI} ,所以你可以做的唯一的事情是违反%{THE_REQUEST} <匹配/露面code>变量:

When you type in your browser: http://domain.com////////file.txt, the rewrite engine only sees as the URI: /file.txt. However, if you type in your browser: http://domain.com///path////to/////file.txt, the rewrite engine sees the URI as /path////to/////file.txt. The extra leading slashes never show up in the %{REQUEST_URI}, so the only thing you can do is match against the %{THE_REQUEST} variable:

RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /[/]+
RewriteRule ^ %{REQUEST_URI} [L,R=301]

这只会满足浏览器的地址栏结束了额外的斜线开头,而不是迷失在某个地方的URI最终双斜杠。为了照顾那些,你需要做这样的事情:

That will only satisfy the extra leading slashes that end up on the browser's address bar, and not stray double slashes that end up somewhere in the URI. To take care of those, you'll need to do something like this:

RewriteCond %{REQUEST_URI} ^(.+)//(.+)$
RewriteRule ^ %1/%2 [L,R=301]

这篇关于我301怎么可以重定向多个领先的斜杠(///file.txt)只是(/file.txt)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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