开发者

Problem using .htaccess to replace characters in URL

开发者 https://www.devze.com 2023-01-14 23:42 出处:网络
I\'ve tried dozens of different ways of doing this but can\'t get any of them to work. My .htaccess does a few things, like setting a custom 404 and blocking image hotlinking. I want to do two things

I've tried dozens of different ways of doing this but can't get any of them to work. My .htaccess does a few things, like setting a custom 404 and blocking image hotlinking. I want to do two things on the URL: add www. if it isn't there (rather annoying Facebook login can't cope with two different sources!), and replacing // with / except after http:.

I've tried this:

# Replace // with /
RewriteCond %{REQUEST_URI}     (.*)(?<!http:)\/{2,5}(.*)
RewriteRule .*                 %1/%2 [R=301,L]

And this:

# Replace开发者_StackOverflow社区 // with /
RewriteCond %{REQUEST_URI}     (.*).com\/\/(.*)
RewriteRule .*                 %1.com/%2 [R=301,L]

And all sorts of permutations. Can anybody tell me what I'm doing wrong?

I need to do this because sometimes multiple /s are being inserted between the .com and the rest of the URL.

Thanks


I don't think http:// is part of REQUEST_URI at all (or of any other environment variable for that matter). It will get parsed out by the browser, and used to determine the nature of the request, long before the actual request is made.

I can be wrong, but I think this is not fixable on htaccess level. The link would have to be properly formatted in the first place.

Update: Looking at the information Apache passes on to PHP, I think I'm right. The protocol used to make the request is not part of the URI components we get to play with.


Here's how to force www.:

<IfModule mod_rewrite.c>
#Add WWW
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
#End Add WWW
</IfModule>

Considering what @Tim mentioned below, I would check %{REQUEST_URI} if it contains //, and that would be my RewriteCond:

<IfModule mod_rewrite.c>
#Replace // with /
RewriteCond %{REQUEST_URI} // [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
#End Replace // with /
</IfModule>


I'm not sure why you're experiencing trouble with the multiple slashes, since it should be able to resolve the file either way. However, it is possible to check for and remove them with a redirect (I've combined this with your force-www so there's at most one external redirection):

RewriteCond %{THE_REQUEST} ^[A-Z]+\s[^\s]*/{2,} [OR]
RewriteCond %{HTTP_HOST}  !^www\.
RewriteCond %{HTTP_HOST}   ^(www\.)?(.*)$
RewriteRule ^ http://www.%2%{REQUEST_URI} [R=301,L]

Note that %{REQUEST_URI} has the duplicate slashes removed (only in mod_rewrite, this isn't true for scripts later on), so we can use it in the redirect to automatically take care of that issue for us. The original request will still have the multiple slashes though, so we check for them by examining %{THE_REQUEST}.

0

精彩评论

暂无评论...
验证码 换一张
取 消