Remove WWW prefix from your website - url

How does Stack Overflow (and other web sites) remove the 'www' prefix when it's entered as part of a URL?
Is it a redirect, a rewrite or something else entirely?
Update: I'd specifically like to know in the context of IIS 6

On Apache, it looks like this (inside an .htaccess file):
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]

An easy way to do this is using the Apache "Redirect" directive:
<VirtualHost *:80>
ServerName www.example.com
Redirect permanent / http://example.com/
</VirtualHost>
<VirtualHost *:80>
ServerName example.com
# remainder of server configuration goes here
</VirtualHost>
The Redirect directive automatically preserves anything following the / in the URL. I find this method easier to read and understand than the Rewrite method.

Firing up Fiddler, we can see that the server responses with a "301 Moved Permanently" status and refers it to http://stackoverflow.com .
Since StackOverflow is hosted on Windows 2k8 IIS7 they set up this redirect straight away in IIS7.
FYI:
a list of HTTP statuses
If you are a .NET developer you might know "Respose.Redirect" , this creates a 302 Object Moved status. Search engines like 301 status codes in this case better, because they know they should not come back to www.stackoverflow.com in the future.

You can do it several ways, using mod_rewrite and redirecting is my favorite. Something like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.cuenca.co$ [NC]
RewriteRule ^(.*)$ http://cuenca.co/$1 [R=301,L]

redirect. the sub-domain "www.stackoverflow.com" would simply redirect to "stackoverflow.com".

You need a default dns entry added pointing to your web server.
ping site.com and verify ip is pointing to webserver, if not you need to get the default DNS entry added.
for a basic setup:
You'll have to add host headers http://www.visualwin.com/host-header/
Create 1 site with a hostheader of www.site.com
In the Home Directory tab, set it to a permanent redirect to http://site.com
Create a 2nd site with a host header of site.com
If you want www.site.com/file.html to redirect to site.com/file.html you will need a more advanced setup with something like ISAPI_Rewrite or use custom 404 pages to do it.

You can do what mod_rewrite does for Apache, with a comparable URL rewriter for IIS. A good one is IIRF. The rule is:
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [I]
RedirectRule ^(.*)$ http://example.com/$1 [R=301]
You can also wildcard the hostname like so:
RewriteCond %{HTTP_HOST} ^(.+)\.example\.com$ [I]
RedirectRule ^(.*)$ http://example.com/$1 [R=301]
IIRF is free to use.

For apache
<VirtualHost *:80>
ServerName yourdomain.tld
ServerAlias www.yourdomain.tld

Related

Rails - .htaccess not forcing users to use https

I want user to only be able to access the https version of the site. I figured out that a way to do this is with a .htaccess file. I've have added the SSL cert and thats all working fine.
I made the .htaccess file and added the following code in it (replacing 'example' with the domain name)
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]
I deployed it and visited http://example.com/ and it loaded just fine as http.
I dont know if because its a rails app that I might need to put the .htaccess file in the public folder, or if there is more I need to do. But from what I read this should work.
I'm using google cloud platform to host my site, and I cant find the VM server type so maybe its not the right server type to run .htaccess files on?
I'm hoping someone here knows how to get this working.
With .htaccess try the following:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
However, with Rails you can use the following in your environment config:
config.force_ssl = true

Remove "www" from all links to my website - nginx

I created a Rails app and I would like when I put any link pointing to my website, the "www" to be removed.
For example when someone enters http://www.mydomain.com/users into the browser address, this to be changed into http://mydomain.com/users when the page loads up.
So if I was working with apache I could add this lines to .htaccess file:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
However I am working with nginx and unicorn (I am not sure if unicorn has something to do with this).
Do I place something like that in my nginx configuration?
Any resource / point to the right direction will be really helpful.
As #Ricoxor answered, you could use a rewrite to get the correct functionality; however, this is listed as an examplary pitfall in the Nginx Pitfalls documentation.
The correct way to do this would be to write multiple server directives as shown by the documentation:
server {
server_name www.domain.com;
return 301 $scheme://domain.com$request_uri;
}
server {
server_name domain.com;
[...]
}
The reason this is more 'correct' is described at the page linked:
There are actually three problems here. The first being if directives.
That's what we care about now. Why is this bad? Did you read If is
Evil? With if directives Nginx is forced to evaluate every request for
all domains. Evaluating every request against if directives is
extremely inefficient. Avoid using if directives and use two server
directives as shown.
Try that :
Apache :
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.yourDomain\.com$ [NC]
RewriteRule ^(.*)$ http://yourDomain.fr/$1 [QSA,R=301,L]
Nginx :
# nginx configuration
location / {
if ($http_host ~* "^www\.yourDomain\.com$"){
rewrite ^(.*)$ http://yourDomain.fr/$1 redirect;
}
}

How to rewrite urls

I have a site at xyz.com, for some reasons I want to redirect access to www.xyz.com to the url xyz.com. Even if they add 'www', I want to alter the address in their bar to xyz.com. Is there a way?
First enable mod_rewrite in your web server then use
.htaccess file in your directory with this content:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.xyz\.com$
RewriteRule ^(.*)$ "http\:\/\/xyz\.com\/$1" [R=301,L]

creating wildcard based subdomain [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Apache rewrite based on subdomain
I am stuck with one problem which not able to solve it. Plz help me to come out of this.
My requirement is:
I want to have each city name as sub domain in my site. Say www.mysite.com, which can have www.delhi.mysite.com, www.bangalore.mysite.com. The list might go endless. My problem is I don't want to create folders for each of the sub domains. I want to handle it in the URL as query string, say www.mysite.com?city=bangalore. This way I can redirect the request to a single file.
I have made the set up LAMP architecture.
In vhost file, it is
<VirtualHost 127.0.0.1:80>
ServerAdmin webmaster#localhost
ServerName www.mysite.com
DocumentRoot /var/www
</VirtualHost>
<VirtualHost 127.0.0.1:80>
ServerAdmin webmaster#localhost
ServerName *.mysite.com
DocumentRoot /var/www
</VirtualHost>
and Also updated .htaccess
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.mysite\.com [NC]
RewriteCond %{HTTP_HOST} ([^.]+)\.mysite\.com
RewriteRule ^(.*)$ http://www.mysite.com/index.html [R,L]
But still I am getting 404 error.
Is my approach correct?? Can it be achievable??
Please help me in doing this.
Try something along these lines:
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com$
RewriteRule ^/(.*)$ http://www.domain.com/%1/$1 [L,R]
plus a rule to not apply this to the www prefix.
And next time, please use the search function, this has been asked before.
Make sure you have enabled the rewrite module. Try putting the statements into your vhost config instead of .htaccess.

what's the best way to force a domain with a rails app? ex: example.com instead of www.example.com

My rails app seems to break when it answers on www.example.com, it previously was working fine with just example.com...however I've moved servers recently and would like to know the best way to redirect all www.example.com requests to go to http://example.com/.../
thanks.
This should do the trick, assuming that you have mod_rewrite enabled
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.example\.com
RewriteRule ^(.*)$ http://example.com$1 [R=301,L]
It depends on your server setup and there are different ways. You can just cname www in DNS to the root domain, for one way. If you are running Rails behind Apache then you can do it in Apache with mod_rewrite. If you are taking network requests straight into Rails with eg, mongrel (or webbrick) then you may have to configure those servers, or might have to use Rails routes ?
Hope that helps,
adricnet
According to the Apache's name-based virtual host documentation the first virtual host is the default host. I use this to make the first entry a catch-all that redirects every "undefined" request to the main site:
# Default catch-all
<VirtualHost *:80>
# Note the lack of a ServerName
RewriteRule ^(.*)$ http://www.example.com$1 [redirect=permanent]
</VirtualHost>
# Site 1 - www.example.com
<VirtualHost *:80>
ServerName www.example.com
[ the rest of the site config ]
</VirtualHost>
For extra credit you can set up a wildcard DNS entry so that every undefined host (e.g. asdfasdfasdfas.example.com) gets redirected to www.example.com.
You might not want to hear it but I suggest fixing what breaks instead. You've hard coded your domain somewhere in your app, probably in your routing but it is not possible to tell without the specifics of the error(s), and you need to remove that so you, or some other maintainer, won't have to deal with it again in the future.

Resources