Forum Moderators: phranque

Message Too Old, No Replies

DNS sub-directory help

         

mike2010

5:16 pm on May 12, 2012 (gmt 0)

10+ Year Member



I was looking for a DNS help section, but I guess this is the closest related..

I've got one of my domains (php driven) where people are accessing it from a subdomain version, even though I don't have it specified like that anywhere on the net.

example , the site should be accessed such as :

www.mysite.com/currentnews.php

instead it's being occasionally accessed - (and even indexed in Google already as)

local.mysite.com/currentnews.php

^not the actual , real links

Since I don't have any subdirectories on the domain and won't have any in the future...is there a way I could force these users (to redirect) to use the correct www. no matter what subdirectory extension they try to access the site through ?

g1smd

5:24 pm on May 12, 2012 (gmt 0)

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



Yes. Two lines of mod_rewrite code issuing a 301 redirect will do it.

If requested HOST_NAME is not exactly www.example.com redirect to www.example.com plus originally requested page.

The code is in hundreds of previous posts here. Let's see your best effort.

mike2010

12:35 am on May 13, 2012 (gmt 0)

10+ Year Member



redirect 301 / http://www.example.com/

?

g1smd

12:51 am on May 13, 2012 (gmt 0)

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



Redirect comes from mod_alias. That's the wrong module.

You'll need to use mod_rewrite syntax here, and test the requested hostname, not just the requested path.

mike2010

1:15 am on May 13, 2012 (gmt 0)

10+ Year Member



2nd try..

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

mike2010

1:31 am on May 13, 2012 (gmt 0)

10+ Year Member



bah, it still doesn't work.. I give up.

I'm guessing it's something along these lines though ?

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

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


Looked through 10 different sites just now, still didn't have the solution I needed.

basically I want everything that is subdomain.example.com to always go to the www.example.com version

phranque

3:07 am on May 13, 2012 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



this:
If requested HOST_NAME is not exactly www.example.com



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

lucy24

3:25 am on May 13, 2012 (gmt 0)

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



Darn it, phranque, we were going to force him to work it out for himself :-P

Anyway, you're close but no cigar.

Mike, are you on shared hosting? Check your control panel and/or have a chat with the host. They're not supposed to be making up subdomains out of thin air. (Mine doesn't. I've tried it, and the browser comes back sulking that it can't find the domain.)

g1smd

6:19 am on May 13, 2012 (gmt 0)

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



Lose the "[NC]" flag as that interferes with the "exactly" logic by allowing aNyCase.

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


The "(" ")" "?" part stops an infinite redirect loop for pure HTTP/1.0 requests as those do not send a HOST header.

[edited by: incrediBILL at 9:34 pm (utc) on May 13, 2012]
[edit reason] fixed RewriteCond syntax per g1's request [/edit]

phranque

7:32 am on May 13, 2012 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



hostnames are case-insensitive.

http://tools.ietf.org/html/rfc1034#section-3.1:
By convention, domain names can be stored with arbitrary case, but domain name comparisons for all present domain functions are done in a case-insensitive manner


plenty of examples here using the [NC] flag.
site:webmasterworld.com RewriteCond HTTP_HOST NC:
http://www.google.com/search?q=site%3Awebmasterworld.com%20RewriteCond%20HTTP_HOST%20NC [google.com]

also g1smd described something (the "?") that is missing in the rewrite code example:

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

lucy24

11:51 am on May 13, 2012 (gmt 0)

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



By convention, domain names can be stored with arbitrary case, but domain name comparisons for all present domain functions are done in a case-insensitive manner

But that's at the DNS level isn't it? mod_rewrite just deals with strings and labels and Regular Expressions; it doesn't know from internet addressing conventions.

mike2010

6:49 pm on May 13, 2012 (gmt 0)

10+ Year Member



Darn it, phranque, we were going to force him to work it out for himself :-P


I've been tortured enough.

no worries of shared hosting, I run the server actually.

but could someone change my 'join date' to something in 2012 ? :-/

Not sure what the extra question mark was for, but it wasn't needed. (thx phranque)

for some of the PHP directories that wouldn't listen, I successfully added a separate .htaccess in /phpdirectory/ and changed to -

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

thx all.

g1smd

7:21 pm on May 13, 2012 (gmt 0)

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



The question mark prevents an infinite redirect loop when your server receives a pure HTTP/1.0 request.

Watch the "Error 500" entries fill your server error log for HTTP/1.0 requests.

phranque

9:40 pm on May 13, 2012 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



keep the question mark and the parentheses - you likely won't have an easy way to test it but they are necessary.