Mobile app version of vmapp.org
Login or Join
Correia994

: Setting rewrite rules in httpd.config file at host server 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

@Correia994

Posted in: #Htaccess #HttpdConf #Php

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 example.com with this code:

RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} ^(www.)(.+) [OR]
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^(www.)?(.+)
RewriteRule ^ %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?

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Correia994

1 Comments

Sorted by latest first Latest Oldest Best

 

@Ogunnowo487

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 example.ma and the server is able to match this) it doesn't really matter whether 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 example.com ServerAlias example.ma example.ma ServerAlias example.fr 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 ^ %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 example.com ServerAlias example.ma example.ma ServerAlias example.fr example.fr
# Redirect everything to the canonical domain
Redirect / example.com/ :

<VirtualHost *:80>
ServerName example.com
UseCanonicalName On
:

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme