문제

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

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

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

Can you help?

도움이 되었습니까?

해결책

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]

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]
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top