Forum Moderators: phranque

Message Too Old, No Replies

Turning off redirect not working

works on local apache 2 but not on remote 1.3

         

RedAndy

9:38 pm on Mar 16, 2006 (gmt 0)

10+ Year Member



Hi,

most of my rewriting is working fine but I have a problem with the two sections I have marked in bold below. They simply don't happen - the server behaves as if the instructions don't exist at all.

In the previous version of the site the non-www to www worked fine, so it's probably not the server - but it's cheap hosting so I have no control.

The other thing I need it to do is _not_ redirect when the /guiding folder is requested.

Both of these work just fine on my 2.0.x server but not on the hosting companies 1.3, I don't know if that's relevant.

I'd be very grateful for any help you could give me as I spent a lot of time making changes and uploading last night and today without any luck,

many thanks,

Andy

This is the entire .htaccess file in root. There are no others in any subdirectories apart from /pages.


php_flag session.use_only_cookies On
php_value session.use_only_cookies 1
php_flag zlib.output_compression On
php_value zlib.output_compression_level 1

IndexIgnore *

#Old redirects from pages with extensions - the rest is handled by the DB when a 404 is requested
#or in the /pages directory
RedirectPermanent seakayaks.html http://www.example.co.nz/products/kayaks/sea_kayaks/
RedirectPermanent msk.html http://www.example.co.nz/products/kayaks/multi_sport_kayak/
RedirectPermanent /kayak_dvds/sick_line_3_-_the_story_unfolds http://www.example.co.nz/products/books_dvds/kayak_dvd/sick_line_3__the_story_unfolds

#Ensure WWW
[b]RewriteEngine On
RewriteCond %{HTTP_HOST} [i][/i]!^www\.example\.co\.nz
RewriteCond %{HTTP_HOST} [i][/i]!^example
RewriteRule (.*) http://www.example.co.nz/$1 [R=301,L][/b]

RewriteEngine on
RewriteCond %{REQUEST_URI} ^/[a-z0-9_/]{3,255}$
[b]RewriteCond %{REQUEST_URI} [i][/i]!^/guiding[/b]
RewriteRule ([a-z0-9_/]{3,255}) index.php?url=$1

RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^$ index.php

ErrorDocument 404 /index.php?url=404

[edited by: jdMorgan at 2:17 am (utc) on Mar. 17, 2006]
[edit reason] examplified. [/edit]

RedAndy

10:40 pm on Mar 16, 2006 (gmt 0)

10+ Year Member



Sorry - the /guiding turned out to be me doing things late at night and not checking them in the morning - I didn't upload the relevant directory so it gave me a 404...

The other problem still stands.

jdMorgan

7:03 pm on Mar 17, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You have an extra RewriteCond which is defeating the usual purpoe of this (corrected) code:

# Ensure WWW
RewriteEngine on
# If requested hostname is NOT www.example.co.nz
RewriteCond %{HTTP_HOST} !^www\.example\.co\.nz
# Redirect to www.example.co.nz
RewriteRule (.*) http://www.example.co.nz/$1 [R=301,L]

Jim

RedAndy

6:59 pm on Mar 18, 2006 (gmt 0)

10+ Year Member



argh! So it is, thanks Jim, I changed the name of the local server from 'ex' to 'example' and changed the condidition to match without recognizing the problem that would cause. I've appended a $ to the end of the condition and is all good again,

many thanks, I couldn't see it for looking,

Andy

jdMorgan

7:47 pm on Mar 18, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> I've appended a $ to the end of the condition and is all good again

If you mean you've added an end-anchor to a hostname, then be careful, as that will break the rule if a port number is appended (which is a perfectly-valid thing to do). End-anchored hostnames must account for this to avoid rare-but-hard-to-identify problems. Here's an example of a properly-end-anchored domain check:


RewriteCond %{HTTP_HOST} ^(www\.)?example.com(:[0-9]+)?$

Jim

RedAndy

8:16 am on Mar 19, 2006 (gmt 0)

10+ Year Member



Thanks for the warning, that hadn't occured to me. I don't think it will be a problem as I only appended it to the second condition. The relevant rules now read


RewriteCond %{HTTP_HOST}!^www\.example\.com
RewriteCond %{HTTP_HOST}!^example$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

So, if I ask for my mirror as example:80 then I'll get redirected to www.example.com. I guess I don't really want that, but I can live with it as I'm unlikely to bother typing the port number. Otherwise example.com:80 will redirect while www.example.com:80 won't. I think I'm okay :o)

Thanks again for your help Jim, very much appreciated and it's always good to get some more regex tips

Andy

jdMorgan

2:28 am on Mar 21, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm not sure what you are trying to do with the second RewriteCond. But it will have no effect whatsoever, because it is an invalid hostname, and so will never occur in the HTTP_HOST variable. Since the match is negated by "!" the second RewriteCond will *always* be true, and thus have no effect.

RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} !^example$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

In addition to whatever change you intended to make with the second RewriteCond abive, I recommend the following code modification to avoid trouble with old HTTP/1.0 clients:

RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

Jim

RedAndy

12:15 am on Mar 24, 2006 (gmt 0)

10+ Year Member



Hi,

The second rule is working just fine - I have a local host named "example". It's only available on the Lan, its inclusion in the conditions saves having to maintain different .htaccess files localy and remotely. On the live site example.com now redirects to www.example.com but my local server ( example ) does not.

What's the issue with 1.0 clients? I've not seen your first condition anywhere before - it seems to say that 1.0 clients don't declare/pass a host? But doesn't the server know what name a resource is being requested from? This is a virtual server environment so surely it has to be stated somewhere?

Many thanks for your continuing help,

Andy

jdMorgan

12:57 am on Mar 24, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> But doesn't the server know what name a resource is being requested from?

True HTTP/1.0 clients do not send a Host header, and in that case, the answer to your question is "No, the server won't know." In most cases, true HTTP/1.0 requests won't ever reach a name-based virtual server because of this fact.

But it may happen that your server is set up as the default server, in which case the request will 'land' in your account. If you don't check for a non-blank hostname header, and if it is blank, it will then match any negative-pattern hostname check, and put your server into an 'infinite' redirection loop.

Although the server eventually stops redirecting when it hits the MaxRedirects limit, the client may come back and issue another request. In some cases, I've seen hosting accounts disabled and even terminated because of the resulting bandwidth and CPU usage... In short, that blank-check is 'cheap insurance' for code posted here that may be copied globally...

You probably have seen this posted elsewhere, although it's usually coded as


RewriteCond %{HTTP_HOST} !^$

which is entirely equivalent, but my method is shorter/faster... :)

It also saves having to work-around the fact that WebmasterWorld deletes required spaces between "}" and "!".

I like robust, fast code.

Jim

RedAndy

1:57 am on Mar 24, 2006 (gmt 0)

10+ Year Member



Awesome, thanks for the explanation Jim, your default server reasoning makes perfect sense. I hadn't made the connection between!^$ and . , I suspect I can use that on a bunch of my sites. Speed and stability are always good :)

Thanks,

Andy