2

I have either of the following URLs: http://www.scenemovie.org or http://subdomain.scenemovie.org

I want to keep "scenemovie.org" and remove "http://www.". How can this be accomplished with jQuery? Thanks!

nbrooks
  • 18,126
  • 5
  • 54
  • 66
user1675643
  • 23
  • 1
  • 3
  • 2
    Do you have that URL in a string and you wish to modify that string, or is that the URL of the page where your javascript runs and you wish to redirect to the same URL without the `www.`? – lanzz Sep 16 '12 at 12:54
  • 2
    You can use `host` property: `var host = location.host;` – Ram Sep 16 '12 at 12:59
  • possible duplicate of [How do I parse a URL into hostname and path in javascript?](http://stackoverflow.com/questions/736513/how-do-i-parse-a-url-into-hostname-and-path-in-javascript) – Felix Kling Sep 17 '12 at 04:07

4 Answers4

5

If there is a sub-subdomain, then this regex return only domain:

url = 'http://a.sub.subdomain.exemple.com';
domain = url.match(/[^\.]*\.[^.]*$/)[0];
ManUtopiK
  • 4,495
  • 3
  • 38
  • 52
2
url = 'http://www.scenemovie.org';
domain_and_tld = url.replace(/http:\/\/.+?\./, '')
Ivan Ivanic
  • 2,982
  • 1
  • 20
  • 21
1

How to do that with jQuery?

In the general case where you may not know what the required domain is, you can't.

There's no specification for what constitutes a "subdomain" as you've used the term in the DNS. In effect what you're looking for is an administrative boundary within the DNS, but there's no deterministic way to tell where those administrative boundaries are.

For example, I happen to know the owner of www.me.uk. That is a correctly registered domain name in its own right, even though it looks like a "subdomain".

The closest you can get (although even that is flawed) is to compare the domain against the "Public Suffix List", which is an attempt to create a definitive list (it isn't) of which domains are "registerable domains".

Alnitak
  • 334,560
  • 70
  • 407
  • 495
-1

Try htaccess:

<IfModule mod_rewrite.c>
  RewriteCond %{HTTPS} !=on
  RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
  RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
</IfModule>
yckart
  • 32,460
  • 9
  • 122
  • 129