0

I have read a number of formulas for redirecting example.com to www.example.com and this appears to apply to .htaccess. However, I am confused about how this might work.

I want to assume that I have no access to the Apache vhosts configuration, and that I need to do it with .htaccess.

Suppose the configuration contains something like this:

<VirtualHost *:80>
    ServerName www.example.com:80
    ServerAlias www.example.com
    VirtualDocumentRoot /whatever/example.com/www
</VirtualHost>

One such formula is something like this"

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ www\.%{HTTP_HOST}/$1 [R=301,L]

However, I don’t see how one root directory can respond to both requests.

The question is: Is it even possible to redirect example.com to www.example.com using .htaccess only, without tweaking the Virtual Host file?

The way I see it, the original request

I run my own server, and I can do anything I like. However, I am asking this because many of my clients and students have no access to tehe configuration, and can only fiddle with .htaccess.

Manngo
  • 14,066
  • 10
  • 88
  • 110
  • Certainly that is possible. Or do you think all tutorials in the internet are just plain false? Reason why that _does_ work is because 1. the two hosts do _not_ have to be served by the same http server, but mostly because there always is a _default_ hosts answering to requests to _any_ incoming host, so especially requests to hosts where _no_ explicit vhost has been configured for. That is the one you are interested in. – arkascha May 01 '17 at 06:26
  • Possible duplicate of [apache redirect from non www to www](http://stackoverflow.com/questions/1100343/apache-redirect-from-non-www-to-www) – Vinnie James May 01 '17 at 06:27
  • @VinnieJames Not really. I’m not asking how to do it, but how does it work? – Manngo May 01 '17 at 06:28
  • @arkascha How does that apply to a virtual host? – Manngo May 01 '17 at 06:29
  • Sorry, I don't understand that question. Incoming requests are served either by a vhost matching the host header or by the default vhost. The request _always_ is answered by some vhost. – arkascha May 01 '17 at 06:31
  • It works by rewriting your request, You request example.com, and the server says NO, instead im going to give you www.example.com Turn up the l[oglevel](https://httpd.apache.org/docs/2.4/mod/core.html#loglevel) if you want to see exactly what is happening – Vinnie James May 01 '17 at 06:32
  • You are right that what you try is not possible if you also have no access to the document folder of that default virtual host. You need access to some place where the non-www request is processed, either the vhost configuration or its document folder so that you can implement dynamic configuration files there. – arkascha May 01 '17 at 06:32
  • @VinnieJames The OP asks how it can be that those rules are applied at all in the sketched scenario. – arkascha May 01 '17 at 06:34
  • @VinnieJames But the `.htaccess` file is presumably _inside_ `/whatever/example.com/www` which means that the server had to look there to find out what to do. But what causes it to look there if the original request `example.com` doesn’t match `ServerAlias www.example.com` in the first place? – Manngo May 01 '17 at 06:34
  • 1
    I answered to that already: that is what the default vhost is for. You want to read the documentation for that. – arkascha May 01 '17 at 06:35
  • Yes, as @arkascha said, it the host is not matched it will server the default vhost which could have been configured to use www.example.com – Vinnie James May 01 '17 at 06:39
  • @arkascha I think our comments are out of sync. So you are saying I _also_ need a `` section that _includes_ `ServerAlias example.com` for this to work? Can I do that in a single `` block? – Manngo May 01 '17 at 06:39
  • No, I did _not_ say that. I only pointed out that there is a default vhost defined in every apache based http server. It does not need any special configuration (though it can have one). And I confirmed that you do indeed need some location to place your rewriting rule above. – arkascha May 01 '17 at 06:41
  • @arkascha In my `httpd.conf` there is a ` DocumentRoot /whatever/ ` section. As I understand it, this is where all unmatched urls will fall, including `example.com` if I don’t have a specific `` block for it. Is that right? Given that I have a number of virtual hosts, I should therefore ensure that I do. Can I combine it with the one in my question? – Manngo May 01 '17 at 06:47
  • Yep, that is basically correct. I do not understand what you mean by "I should therefore ensure that I do". If that refers to a specific vhost also answering to the non-www host name, then the answer is "no", you do not have to take care of that, although you can. You could simply place rewriting rules for non-www host names for _different_ domains in a dynamic configuration file inside the document folder of that default virtual host. So one rewrite rule for each specific virtual host. – arkascha May 01 '17 at 06:50
  • @arkascha Thanks for your patience in all of this. Leaving my `httpd.conf` file as it is, if I include `ServerAlias example.com www.example.com` will it then do the job properly? That is, `example.com` and `www.example.com` both land in the same directory, and `.htaccess` can sort it out. – Manngo May 01 '17 at 06:53
  • Sure, that actually is the typical scenario, to use `ServerAlias` directives for all host names meant to be grouped together. In that case however you should also place those rewriting rules right there in that host configuration instead of using a dynamic configuration file. Those files are notoriously error prone, hard to debug and they really slow down the server. – arkascha May 01 '17 at 06:56
  • @arkascha OK, that has cleared it up for me. My original scenario, in fact, _won’t_ work unless either the `.htacess` file is in the _default_ vhost, or I include `example.com` in the `` section. Could your write an answer so that I can accept it? – Manngo May 01 '17 at 07:00
  • This is correct, that won't work. However you never said that you can _not_ place the rules inside a dynamic configuration file inside the default vhosts document folder. That is why the correct answer to you question is "yes, it is possible". You now narrowed down the requirements. Then indeed this is not possible. – arkascha May 01 '17 at 07:02

0 Answers0