If I have domain name example.com, and if I set an A record for www, www.example.com will return the IP address specified in A record. And what should I configure if I want to resolve example.com (without www.) to an IP address?
Adjust the DNS settings for example.com. Make sure you have address records (type A records) for both example.com and www.example.com, with the same IP address.
If you are using virtual hosting to provide many websites on a single IP address, you will also need to tell your web server about the alternative name for the site. In your web server's configuration, add example.com as an alias for www.example.com. In an Apache server httpd.conf file, this typically looks like:
<VirtualHost *>
DocumentRoot /home/www/web
ServerName www.example.com
ServerAlias example.com
</VirtualHost>
http://www.boutell.com/newfaq/creating/withoutwww.html
Typically you would also redirect:
apache config
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule ^(.*) http://www.example.com/$1 [R=301,L]
Related
My website can be accessed via https://example.com but can not be found using either https://www.example.com or www.example.com, if I use either of these it just throws up a "site cannot be reached" page.
Is this something I can change in the .htaccess file or is it the DNS of the domain?
I have tried adding the following code to the .htaccess file, but it makes no difference
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
...or is it the dns of the domain?
Yes, this is "most probably" a DNS issue. The request is not even reaching your server by the sounds of it. .htaccess is consequently never processed.
You'll require a CNAME record that points the www subdomain to the domain apex (example.com) - so that both www.example.com and example.com point to the same place.
However, you also need to make sure that your server is accepting requests to both www.example.com and example.com - this doesn't happen by default. Although if you have configured your account via some hosting control panel then this has probably already been done for you. Otherwise, you need a ServerAlias record defined in the relevant <VirtualHost> container. For example:
ServerName example.com
ServerAlias www.example.com
I have domain abc.com and I noticed another domain not owned by me pointing to the same IP address as mine. It is ghosting mine, so when you visit that website it looks exactly as if you were on mine.
Any ideas of how to prevent that?
my vhost looks like:
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerName www.example.com
DocumentRoot /path/to/site
<Directory /path/to/site/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
RewriteCond %{HTTP_REFERER} .
RewriteCond %{HTTP_REFERER} !example\.com [NC]
RewriteCond %{HTTP_HOST} !example.com [NC]
RewriteRule .? - [F]
ServerName example.com
ServerAlias www.example.com
</VirtualHost>
</IfModule>
Apache uses the Host field from the HTTP requests headers to know which vhost is requested (Host corresponds to apache ServerName or ServerAlias).
Try apache2ctl -S, it will give you an ouput containing something like:
VirtualHost configuration:
*:80 is a NameVirtualHost
default server ip (/etc/apache2/sites-enabled/000-default.conf:1)
port 80 namevhost ip (/etc/apache2/sites-enabled/000-default.conf:1)
port 80 namevhost www.example1.com (/etc/apache2/sites-enabled/001-vhost.conf:1)
port 80 namevhost www.example2.com (/etc/apache2/sites-enabled/002-vhost.conf:1)
*:443 is a NameVirtualHost
default server 127.0.1.1 (/etc/apache2/sites-enabled/default-ssl.conf:2)
port 443 namevhost 127.0.1.1 (/etc/apache2/sites-enabled/default-ssl.conf:2)
port 443 namevhost www.example1.com (/etc/apache2/sites-enabled/non-default-ssl.conf:2)
Now imagine my ip is 1.1.1.1:
Given those three curl commands:
1. curl http://1.1.1.1 -H 'Host: www.example1.com'
1. curl http://1.1.1.1 -H 'Host: www.example2.com'
1. curl http://1.1.1.1 -H 'Host: www.spoofexample.com'
First one, apache finds the corresponding ServerName in a vhost file and uses /etc/apache2/sites-enabled/001-vhost.conf to satisfy request
Second one, apache finds the corresponding ServerName in a vhost file and uses /etc/apache2/sites-enabled/002-vhost.conf to satisfy request
Third one (your undesired mapped DNS), apache doesn't find the corresponding ServerName into any of its vhosts file, and uses /etc/apache2/sites-enabled/000-default.conf to satisfy request
The same logic applies to SSL vhosts.
PS1: ServerName for the default HTTP vhost has a value of ip, and there is no ServerName in the default SSL vhost. Apache just assumes a 127.0.1.1, which is not the IP address it listens on (just telling so it's not more confusing).
PS2: To make a vhost the default one it must be the first by names sorted (000 -> 001 -> 002).
It is not possible somebody can ghost your account.
Suppose I am domain owner of abc.com from hostgator. In order to host it on godaddy.com I have to go to domain controller put the godaddy nameservers there. Then on hosting i create a website with same name.
You should not hosting a real domain with ipaddress also. always use name servers which will prevent all those configuration.
Please check this page for redirects
https://www.namecheap.com/support/knowledgebase/article.aspx/385/2237/how-to-redirect-a-url-for-a-domain
Are you hosting a domain from your local computer ?
I have a Drupal 7 website, some resources are being requests with the Host set to have the www prefix while others are not.
https://www.example.com when request header is 301 Moved Permanently
https://example.com when request header is 304 Not Modified
Changing the base_url does not appear to modify the behaviour.
Is there anyway I can make drupal set the Host as https://example.com for all the request headers?
There's an Apache wiki page, which shows a solution to your requirement. The first solution uses a virtual host setup and the Redirect directive
# Redirect every request to example.com
<VirtualHost *:80>
ServerName www.example.net
ServerAlias www.example.com
Redirect permanent / http://example.com/
</VirtualHost>
# Define virtual host for example.com
<VirtualHost *:80>
ServerName example.com
DocumentRoot /usr/local/apache/htdocs
</VirtualHost>
And the second setup uses a mod_rewrite redirect
RewriteCond %{HTTP_HOST} !^example\.com [NC]
RewriteCond %{HTTP_HOST} !=""
RewriteRule ^ http://example.com%{REQUEST_URI} [L,R]
You can choose whichever fits your environment best.
I'm trying to redirect one subdomain (a wordpress.com domain) into a folder of another domain. Example:
blog.domain.com to anotherdomain.com/blog
I know that is possible by using .htacces files, but i dont have a hosting service on my first domain.
Is it possible to do it by using DNS?
I have tried by creating some A and CNAME registrations but I cant find a way to do it: Can I have a ip for a specific folder of my second domain?
Thanks :)
I attempted to do a comment but it's too much. You can do this if you own both domains. If you don't have hosting on the first domain but you do on the other domain, then you can add the main domain as an ServerAlias on the other server.
Either through your control panel or your vhost config do the following.
Add an A record for blog.domain.com in DNS pointing to the IP address of the other domain with the hosting account.
In the configuration of the web server with the hosting add a ServerAlias of blog.domain.com to the anotherdomain.com vhost config.
in the root of the anotherdomain.com put this code inside your .htaccess file.
If you want it to actually redirect then you can do this
RewriteEngine on
RewriteCond %{HTTP_HOST} ^blog\.domain\.com [NC]
RewriteRule ^(.*)$ http://anotherdomain.com/blog/$1 [R=301,L]
If you don't want it to redirect and keep blog.domain.com in the address bar then, you can do this.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^blog\.domain\.com [NC]
RewriteRule ^(.*)$ /blog/$1 [L]
I have a website let's say www.example.com and I have a subdomain something.example.com
Both main domain and sub domain are pointing to directory "public_html"
I have .htaccess on root which redirects any URL without www to www.
For e.g. if user enters example.com then he will be redirected to www.example.com
If user enters example.com/mypage.html then he will be redirected to www.example.com/mypage.html
Now the problem is this is also affecting my subdomain. Because if someone enters something.example.com/visit.html then he is redirect to www.example.com/visit.html
I don't want this! If user enters subdomain then I don't want to redirected to www domain. This is what I have in my .htacces file
RewriteCond %{HTTP_HOST} !^www.stackoverflow.com$ [NC]
RewriteRule ^(.*)$ http://www.stackoverflow.com/$1 [R=301,L]
Can you please tell me what should I do to solve above problem?
Thanks.
Do you have access to your webserver configuration? This is better done by configuring the webserver.
On apache you would configure one virtual domain like this:
<VirtualHost *:80>
ServerName somedomainiwanttoredirect.com
ServerAlias maybe.somemoredomainstoredirect.com
ServerAlias orsubdomains.toredirect.com
RewriteEngine On
RewriteRule ^(.*)$ http://www.target.com/$1 [R=301,L]
</VirtualHost>
and on your real configuration, the www.target.com you add your subdomains that you do not want to be redirected:
ServerAlias subdomain.target.com