1

I am a new webmaster. I plan to host my website on a dedicated server. The website has three TLD: .com, .ma and .fr.

I have a couple of rewrite directives in my .htaccess file that force the URL format to http://example.com with this code:

RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} ^(www\.)(.+) [OR]
RewriteCond %{HTTPS} on 
RewriteCond %{HTTP_HOST} ^(www\.)?(.+)
RewriteRule ^ http://%2%{REQUEST_URI} [R=301,L]

Plus other rules. I want to place some of these rules, especially the URL format in the httpd.conf file but I don't know what to write in ServerName and ServerAlias as I have three ServerNames: example.com, example.ma and example.fr. Plus if a user omit www or add it then it's a new ServerName. How can I set these rules in httpd.conf and under what ServerName?

4
  • Presumably, you don't currently have a working site with 3 different domains? (Or is it currently hidden from view on a shared server?) "format to http://example.com" - strictly speaking, that code just removes the www subdomain (if present) on whatever domain is requested (but I expect you know that), it doesn't redirect to http://example.com as you seem to imply in your description. Do these 3 domains serve exactly the same content, or is there some customisation (ie. localisation) for each domain? – MrWhite Aug 31 '17 at 23:07
  • @MrWhite, i don't have a working website yet, the code doesn't remove the www, it redirects to the website without https and www because i use the flag R=301 which means redirect with code 301, and i serve same content without any customisation for each domain – ZeSoft Aug 31 '17 at 23:20
  • I always recommend using the .Htaccess file for common rewrite and blocking. The reason is simple. If the rules are within your httpd.conf file and there is an error, Apache will not start effecting all of the sites negatively. However, if you use .Htaccess and make a mistake, only one site is effected. In principle, you should set up your Apache configuration files once and leave them alone as much as possible. Save the editing for less risky areas. Cheers!! – closetnoc Sep 1 '17 at 16:54
  • Sorry, what I meant was that your description seems to imply that you are canonicalising the hostname (to example.com), but it really just removes the www subdomain on whatever domain is accessed (and yes, removes HTTPS also). (Aside: But why are you trying to redirect from HTTPS to HTTP?) – MrWhite Sep 2 '17 at 0:24
2

Just to clarify usage... you can only have one ServerName directive. If you have more than one then the later directive simply overrides the earlier one. However, you can have many ServerAlias directives. In terms of resolving the requested domain name (ie. you request www.example.ma and the server is able to match this) it doesn't really matter whether www.example.ma is defined as the ServerName or a ServerAlias - the server will find it just the same.

The ServerName doesn't just define the hostname, it can also define the scheme and port number. Whereas, ServerAlias defines only a hostname. Since, in your case, all these domains appear to be using the same scheme and port, it doesn't really matter. (If you had different schemes and ports then you'd be defining separate VitualHosts anyway and each would have its own ServerName - you could still do this.)

The value supplied to ServerName can also be used to construct self-referential redirection URLs. However, this is not the default behaviour (you would need to explicitly set UseCanonicalName On). And if you have multiple domains, then you probably just want to leave it as the default. The default being that it will use the hostname from the request (ie. HTTP_HOST).

Aside: I assume these three domains all point to the same area of the filesystem, run the same scripts and share log files? A single server config or VirtualHost. Otherwise, you might consider separate VirtualHosts for each domain.

So, having said all that, it probably doesn't really matter which domain you set as the ServerName and which domains you set as ServerAlias. However, you must choose one host as the ServerName. To me, the logical one would be example.com (the gTLD and no-www). The ccTLD domains, together with the www variants would be defined as ServerAlias.

For example:

<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
ServerAlias example.ma www.example.ma
ServerAlias example.fr www.example.fr
UseCanonicalName Off
:

Your mod_rewrite redirect would work unchanged in either the VirtualHost or .htaccess context.


However, slight digression...

RewriteCond %{HTTPS} on 
:
RewriteRule ^ http://%2%{REQUEST_URI} [R=301,L]

...why are you redirecting from HTTPS to HTTP?! If anything it should be the other way round. There is no good reason to be redirecting from HTTPS to HTTP in 2017. In order for this to even "work" the SSL certs must be installed anyway!


UPDATE:

i want to redirect to only one domain so it won't be considered duplicate by SEO

However, if you do want to canonicalise the domain name and redirect everything to example.com instead then you would do this slightly differently to the above. Your current mod_rewrite code does not do this.

Instead, you would define two <VirtualHost> containers, one for your canonical domain and one for all the others. Then use a simple mod_alias Redirect to your canonical domain (not mod_rewrite).

For example:

<VirtualHost *:80>
ServerName www.example.com
ServerAlias example.ma www.example.ma
ServerAlias example.fr www.example.fr

# Redirect everything to the canonical domain
Redirect / http://example.com/
:

<VirtualHost *:80>
ServerName example.com
UseCanonicalName On
:
7
  • Thank you @MrWhite for your detailed answer, i really appreciate it, your code of VirtualHost with different ServerAlias answered my question, however you said that you thought i wanted to canonicalise the hostname, and asked about why i would redirect to https, it is because of SEO, because the 3 domains point to the same content, i want to redirect to only one domain so it won't be considered duplicate by SEO, my website doesn't support ssl, so if someone types example.com i want them to be redirect to the default domain without http, am i doing it the right way? – ZeSoft Sep 2 '17 at 12:15
  • "asked about why i would redirect to https" - I asked about why you would redirect to HTTP (not HTTPS). If your "website doesn't support ssl" then you won't be able to redirect to HTTP anyway since users will get a browser error before reaching your application code. "i want to redirect to only one domain so it won't be considered duplicate by SEO" - ok, but that's not what your code is currently doing. If you are canonicalising the domain name (redirecting everything to example.com) then you would probably construct your VirtualHosts / redirect differently to the above. – MrWhite Sep 6 '17 at 14:04
  • I've updated my answer with respect to canonicalizing the domain (as you mentioned in the above comment). – MrWhite Sep 6 '17 at 14:19
  • Thank you @MrWhite for your reply it answered my question perfectly, i don't mean to be annoying but what the UseCanonicalName directive is for? – ZeSoft Sep 7 '17 at 16:43
  • If you have UseCanonicalName On (not the default) then the SERVER_NAME server variable refers to the value of the ServerName directive. Apache uses SERVER_NAME to construct absolute URLs (when it needs to). However, by default SERVER_NAME is the same as HTTP_HOST, which refers to whatever host the user has specified in the request. – MrWhite Sep 7 '17 at 16:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.