Forum Moderators: phranque

Message Too Old, No Replies

Strange problem with htaccess & site records

         

murmy

8:08 pm on Feb 5, 2009 (gmt 0)

10+ Year Member



I am repointing my domain to a new server by changing nameserver site records. Originally the site was on virtual hosting and set up to send all www.domain.com enquiries to domain.com.

For example, if you typed into the browser this page:

[domain.com...]

it would automatically convert in the browser to:

[domain.com...]

I achieved this with a bog standard htaccess file:

RewriteEngine On
Options +FollowSymlinks
RewriteCond %{HTTP_HOST} ^www.(.*)$ [NC]
RewriteRule ^ [%1%{REQUEST_URI}...] [L,R=301]
RewriteCond %{HTTP_REFERER} !^http://domain.com
RewriteCond %{HTTP_REFERER} !^http://www.domain.com
RewriteRule .*[Jj][Pp][Gg]$¦.*[Gg][Ii][Ff]$ [domain.com...] [L]
#domain.com/index.html - your custom hotlinker page

ErrorDocument 404 [domain.com...]
DirectoryIndex index.html

I have to use the IP number in the site records. So far the best I have a achieved is put the IP number in both A records for the www. and the @. The site loads fine and this did work exactly as required but the response time of the site seems to have been affected in a seriously negative way.

Web page speed checkers are showing absurdly long loading times (over 300 seconds for 56k), huge images sizes and it looks as though there is some kind of a conflict with the above htaccess file because whenever I remove the htaccess file and retest (the loading time drops back to 22 seconds for 56k which is what it has been all along - its mostly html with small images).

Should I have used the CNAME record? I tried that first but it didnt seem to work.

I dont know where im going wrong and hours of googling havent yielded so any help on solving this problem would be greatly appreciated.

g1smd

8:27 pm on Feb 5, 2009 (gmt 0)

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



A few notes:

The image file check can likely be simplified to

(jpe?g¦gif)$
with
[NC]
.

You need to escape periods (

.
-->
\.
) used in patterns in conditions.

.

This rule is for a 302 redirect (because it contains a domain name in the target):

RewriteRule .*[Jj][Pp][Gg]$¦.*[Gg][Ii][Ff]$ http://www.example.com/index.html [L]

If you wanted a redirect, change

[L]
to
[R=301,L]
, and do not include "index.html" in the target.

If you wanted an internal rewrite, omit the protocol and domain name, leaving the filepath and filename.

jdMorgan

8:59 pm on Feb 5, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can't rewrite an image to a page anyway -- It won't work.

Your DNS zone file needs to have "domain.com." and "www.domain.com." "A" records defined at minimum. You can also define "@" if you want, but using that *will* be slower. Also, note the trailing periods on these domains -- they are required to specify an FQDN (Fully Qualified Domain Name) in DNS.

Instead of defining "www.domain.com." you may want to use "*.domain.com." to pick up all other possible subdomains as well.

Jim

[edited by: jdMorgan at 9:01 pm (utc) on Feb. 5, 2009]

murmy

10:47 pm on Feb 5, 2009 (gmt 0)

10+ Year Member



Thanks for the feedback guys.

Note that my domain is using the domain registrar nameservers (if that makes any sense). So assuming the server IP is 55.55.555.555 where would I put it in the zone file and how would i place those two defined items?

Forgive me as im a bit out of my depth here and dont exactly know what im talking about but would this work:

@ >>>>> 55.55.555.555 >>>>> A Record
www. >>>>> domain.com. >>>>> A Record
www. >>>>> *.domain.com. >>>>> A Record

(the arrows separating the sections)

Incidently, when I checked the speed test site, it seems that they are labelling all images on the site as the same filesize as the html of the site. I just dont get it.

As soon as i remove the htaccess, that problem is gone. Do you think it has anything to do with the hotlinking code - would it need a reference to the server IP in it? The same code worked fine on the virtual hosting but doesnt seem to work on this dedicated server.

jdMorgan

12:09 am on Feb 6, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Your hotlinking code specifically requires a referer, and that referer must be your own site.

Any request --including the speed tester's-- that does not include a referer will be rewritten to your htmnl page. As noted, this won't work with a browser, because browsers include images using <img src> tages, and a <img src> tag cannot be used to load an HTML page.

I can't answer the question about your DNS control panel -- They vary in design and usage. You'd do well to ask your host.

Jim

murmy

9:26 am on Feb 6, 2009 (gmt 0)

10+ Year Member



Three questions:

(1) What are the consequences of not defining "domain.com." and "www.domain.com." for the "A" records. At the moment, I just have put the IP number of the server in the two A records (in my domain registrar DNS zone file) for @ and www. and the page is loading fine apart from the large images problem.

(2) The htaccess code was working fine on the virtual hosting (which we used for 4 yrs) and has not been altered at all, so why didnt we experience the re-writing of images to html page problem there.

(3) If the images are being rewritten to my html page and this is only happening when a non browser viewer is seeing the page, and seeing as it wont work anyway for a normal browser how should I alter that code to keep the anti-hotlinking but lose the html rewriting. Would this work:

RewriteRule .*[Jj][Pp][Gg]$¦.*[Gg][Ii][Ff]$ [domain.com...] [L]

to

RewriteRule .*[Jj][Pp][Gg]$¦.*[Gg][Ii][Ff]$ [L]

(just removed the url)

Would that solve the problem?

g1smd

9:39 am on Feb 6, 2009 (gmt 0)

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



The image file check can be simplified to
(jpe?g¦gif)$
with
[NC]
.

jdMorgan

4:30 pm on Feb 6, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



1) You force "domain.com" and "www.domain.com" requests to do two DNS lookups per request, the first to find that domain.com refers to "@" and the second to find that "@" refers to 55.55.55.55

2) Truthfully, I don't know. Secure browsers won't allow an <img src> tag to load an .html page. Imagine the exploits possible if that were allowed.

3) Two options for anti-hotlinking: Forbid the access or serve an alternate image:


# Forbid hotlinked image requests (allow blank referrers for AOL, Earthlink, and corporate caching proxies)
RewriteCond %{HTTP_REFERER} .
RewriteCond %{HTTP_REFERER} !^http://(www\.)?example\.com [NC]
RewriteRule \.(jpe?g¦gif)$ - [F]


# Serve an alternate image in response to hotlinked image requests (allow
# blank referrers for AOL, Earthlink, and corporate caching proxies)
RewriteCond %{HTTP_REFERER} .
RewriteCond %{HTTP_REFERER} !^http://(www\.)?example\.com [NC]
RewriteCond %{REQUEST_URI} !/path-to-alternate-image\.(jpe?g¦gif)$
RewriteRule \.(jpe?g¦gif)$ /path-to-alternate-image.$1 [L]

You must completely flush your browser cache before testing either rule, and flush it again between each test from authorized versus unauthorized referrers. If you type in the image URL, your browser will not send a referer header, and the requested image will be served. This exception must be allowed, unless you want all users of caching-proxy ISPs to see your site as broken.

Replace the broken pipe "¦" characters above with solid pipe characters before use; Posting on this forum modifies the pipe characters.

Jim

murmy

1:32 am on Feb 7, 2009 (gmt 0)

10+ Year Member


RewriteEngine On
Options +FollowSymlinks
RewriteCond %{HTTP_HOST} ^www.(.*)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_REFERER} .
RewriteCond %{HTTP_REFERER} !^http://(www\.)?domain\.com [NC]
RewriteRule \.(jpe?g¦gif)$ - [F]

ErrorDocument 404 http://domain.com
DirectoryIndex index.html

Thanks so much guys, youve been so helpful.

The above code (using jims mod) is what I settled on and after extensive testing it does seem to have solved that images re-writing to html problem whilst also protecting the images from hotlinking very effectively.

Two last related questions:

(1) How do I modify the above htaccess code so as to *also* forward any subdomain enquiries to http://domain.com so that if someone tries http://mail.domain.com it still goes to http://domain.com. Basically I want all subdomain attempts sending the visitor to the http://domain.com but I dont want to lose any of the functionalities i already have.

(2) If I add the feature in (1), is there anything i could do when I eventually modify the DNS zone file (to do what jim told me about earlier ie identifying the domain in A records) that could conflict with what im doing in (1).

jdMorgan

3:28 am on Feb 7, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




# Redirect all subdomain requests to canonical non-www domain
RewriteCond %{HTTP_HOST} ^[^.]+\.([^.]+\.[^.]+)\.?(:[0-9]+)?$
RewriteRule (.*) http://%1/$1 [R=301,L]

This new pattern is "<anything-but-a-dot><dot> (<anything-but-a-dot><dot><anything-but-a-dot><dot>) <optional-dot><optional-port-number>", with only the part of the hostname matching the parenthesized sub-pattern back-referenced in the RewriteRule substitution URL. Your original rule could have been 'broken' by simply appending an extra period to indiate an FQDN, by appending a port number, or both.

If you need to support sub-sub-domains or ccTLD domains such as example.co.uk, then let us know. The simple pattern shown works for basic .com-format sub-domains only.

Jim

[edit] Disabled grapical smiley-faces. [/code]

[edited by: jdMorgan at 3:35 am (utc) on Feb. 7, 2009]

murmy

11:27 am on Feb 7, 2009 (gmt 0)

10+ Year Member


RewriteEngine On
Options +FollowSymlinks
RewriteCond %{HTTP_HOST} ^[^.]+\.([^.]+\.[^.]+)\.?(:[0-9]+)?$
RewriteRule (.*) http://%1/$1 [R=301,L]
RewriteCond %{HTTP_REFERER} .
RewriteCond %{HTTP_REFERER} !^http://(www\.)?domain\.com [NC]
RewriteRule \.(jpe?g¦gif)$ - [F]

ErrorDocument 404 http://domain.com
DirectoryIndex index.html

Thats the code I used replacing the old bits with the new bits for the subdomain but it didnt work. I tested a few times, cleared browser cache too. The www. still goes to non-www and the rest functionalities still work but the anything.domain.com doesnt go to domain.com

Did I do something wrong?

g1smd

11:32 am on Feb 7, 2009 (gmt 0)

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



What does it do? What is the HTTP response code you get back? Is there a chain of codes?

murmy

11:50 am on Feb 7, 2009 (gmt 0)

10+ Year Member



Example, if i tried online.domain.com

It just failed and the address bar turns to:

[search.live.com...]

and then the enquiry is in the live.com search box

g1smd

11:54 am on Feb 7, 2009 (gmt 0)

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



Sounds like a DNS problem in that it doesn't resolve at all?

murmy

12:05 pm on Feb 7, 2009 (gmt 0)

10+ Year Member


Im not sure but I dont think its DNS related. http://www.domain.com still goes to http://domain.com and everything else works.

Ive tested the code on another domain in virtual hosting and the same result.... all functionalities work apart from referring anything.domain.com to domain.com

jdMorgan

3:30 pm on Feb 7, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You've apparently got MSN Live catching unresolved domain errors and diverting them to their LIve search engine. Turn that junk off on your "Webmaster work" machines and disable "Show Friendly HTTP Error Messages" in MSIE's Advanced settings so you will see the actual error messages returned by servers, and not Microsoft's dumbed-down (and often useless) error messages."

In order to support wild-card subdomains, your DNS zone file must contain an A record or CNAME similar to

*.example.com. A 192.168.0.1

(Note the required trailing period on the domain)

Changes to DNS will require four hours to propagate to "modern" DNS servers, but may require up to several days to propagate to all DNS servers at all ISPs globally.

Jim

murmy

1:04 pm on Feb 8, 2009 (gmt 0)

10+ Year Member


Hi Jim,

Thanks for that, your solution of:

*.domain.com. A 192.168.0.1

Did actually solve the problem on my virtual hosting where happily, subdomains are forwarding to the domain.com with no problems.

My domain registrar wouldnt accept *.domain.com. in the host record so I was unable to do it on my dedicated server.

One thing, I just wondered why so few large sites seem to be wildcarding their subdomains in some way? Neither of Yahoo, Google, CNN, etc seem to be doing this. Anyone know?

murmy

3:22 pm on Feb 8, 2009 (gmt 0)

10+ Year Member


One other thing... after contacting my domain registrar, I have been able to get the wildcard of subdomains working on my dedicated server.

e.g. http://online.domain.com and hot.domain.com are both going to http://domain.com

However this doesnt work properly:

http://www.super.domain.com/ (the site loads but with images missing)

Is there anything i need to change in this htaccess code:

RewriteEngine On
Options +FollowSymlinks
RewriteCond %{HTTP_HOST} ^[^.]+\.([^.]+\.[^.]+)\.?(:[0-9]+)?$
RewriteRule (.*) http://%1/$1 [R=301,L]
RewriteCond %{HTTP_REFERER} .
RewriteCond %{HTTP_REFERER} !^http://(www\.)?domain\.com [NC]
RewriteRule \.(jpe?g¦gif)$ - [F]

ErrorDocument 404 http://domain.com
DirectoryIndex index.html

jdMorgan

3:47 pm on Feb 8, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That's not a subdomain, it's a sub-subdomain.

In order to support that, your canonicalization and anti-hotlinking routines need to allow for more than one subdomain level.

I should note here that if you intend to change your requirements, then please do so before you ask for help with code. While we are happy to assist with a problem, members here will quickly lose interest if the project continues expanding and requires on-going attention. It is our intent to give you examples from which you can learn to add/modify/expand your own code while referring to the documentation, not to write it and support it for you.

But basically,

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

must become
 RewriteCond %{HTTP_HOST} [b]^([[/b]^.]+[b]\.)+([/b][^.]+\.[^.]+)\.?(:[0-9]+)?$ 

and the back-reference numbers in following RewriteConds and RewriteRules may need to be adjusted to pick up the correct back-reference, since we've now added another set of parentheses. (i.e. %1 now becomes %2)

Jim