Forum Moderators: phranque

Message Too Old, No Replies

Request never completes (.htaccess problem)

         

saquilina

1:18 pm on Jun 13, 2011 (gmt 0)

10+ Year Member



Hi,

I am very new to .htaccess and only know some basics. What I am trying to do is that if I receive the request [hello.mydomain.com...] I redirect to [hello.mydomain.com...]

I am using the following rules and conditions in my .htaccess file;


RewriteCond %{HTTP_HOST} ^hello\.(.+)$ [NC]
RewriteCond %{REQUEST_URI} !^/welcome/ [NC]
RewriteRule ^(.*)$ http://hello.mydomain.org/welcome/ [R,L]


The first condition is to test if subdomain is "hello". The second condition is to test if /welcome/ is already in the request uri. The last condition is to redirect [hello.mydomain.com...] to [hello.mydomain.com...] (if the first two conditions are met).

However my browser is telling me that it "detected that the server is redirecting the request for this address in a way that will never complete". I can't understand why. However by monitoring the http request done by the browser I noticed that after the first request [hello.mydomain.com...] the browser redirects to [hello.mydomain.com...] and then continues doing like that until "max" value in the http header reaches 59.

Does anyone know what I might be doing wrong?

g1smd

1:43 pm on Jun 13, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The (.*) pattern is the FIRST to be evaluated. This says to redirect ALL requests, even those for images, robots.txt, etc. The originally requested filename is not preserved in the redirect. Is that what you want?

You have an infinite redirect loop. What other directives are in the .htaccess file? It is likely they are incorrectly coded and/or in the wrong order.

saquilina

1:55 pm on Jun 13, 2011 (gmt 0)

10+ Year Member



Hi, I was under the impression that the RewriteRule will not be done if any of the RewriteConds before it fail. For example the RewriteRule is not done if I omit the subdomain.

g1smd

2:03 pm on Jun 13, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Yes, that's correct. All must match before it redirects. However, by making the (.*) pattern "too wide" you are maybe redirecting too many requests as well as slowing down the server for requests which are not redirected.

There's also likely unwanted interaction with other rules.

saquilina

2:39 pm on Jun 13, 2011 (gmt 0)

10+ Year Member



I thought ^(.*)$ in RewriteRule meant replace everything with the following (and I give new address).

I think I might have problem with second RewriteCond. I updated script as follows ...


RewriteCond %{HTTP_HOST} ^hello\.(.+)$ [NC]
RewriteCond %{REQUEST_URI} !^/welcome/ [NC]
RewriteRule ^(.*)$ http://google.com/request_uri=%{REQUEST_URI} [R,L]


This means if my request has "hello" as sub-domain but does not have "/welcome/" then go to google.

When I insert [mydomain.com...] I am taken (correctly) to the default page. When I enter [hello.mydomain.com...] I am taken (correctly) to google.com. However when I insert [hello.mydomain.com...] I am still taken to google.com! This is not correct! :(

Also request_uri in the new url is "/missing.html"! At the moment I have no other rules in my file apart from


Options +FollowSymLinks
RewriteEngine On
RewriteBase /

lucy24

10:13 pm on Jun 13, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



For starters: it helps to use "example.com" in your, er, examples. Not for ideological, moral or philosophical reasons but simple mechanics: addresses using example.com are not converted into active links, so we can see exactly what you typed.

59, ouch, sounds like your server isn't using Apache's own 10-redirects default. Better add it yourself before your site has a complete meltdown:

RewriteOptions MaxRedirects=10

Also request_uri in the new url is "/missing.html"!

missing.html is a lot of servers' default name for the 404 page. So by the time the code gets here, it has already established that the page in question doesn't exist and is instead looking for /missing.html in the appropriate domain. (Or subdomain? If your htaccess doesn't say, your server is probably applying its own rules.) This raises the nasty possibility that the mistake is actually in some completely different part of your .htaccess.