0

Is it possible to redirect from the non www version to my website to the www version of the website using httpd.conf ?

Dheeraj
  • 233
  • 1
  • 5
  • 14
  • 1
    possible duplicate of [apache redirect from non www to www](http://stackoverflow.com/questions/1100343/apache-redirect-from-non-www-to-www) – Chandu Mar 01 '11 at 03:31
  • Possible duplicate of [apache redirect from non www to www](https://stackoverflow.com/questions/1100343/apache-redirect-from-non-www-to-www) – zypA13510 Mar 10 '19 at 08:05

3 Answers3

2

Two options:

1.

<VirtualHost *:80>
  ServerName example.com
  RewriteEngine on
  RewriteCond %{HTTP_HOST} ^example.com
  RewriteRule ^/(.*)$ http://www.example.com/$1 [L,R=301]
</VirtualHost>

2.

<VirtualHost *:80>
  ServerName example.com
  Redirect 301 / http://www.example.com
</VirtualHost>

Hope this will help.

Sumoanand
  • 8,835
  • 2
  • 47
  • 46
  • what is the difference between the two options? It looks like the second will only work for the root path and will not redirect, for example, `example.com/stuff.html` to www.example.com/stuff.html`, is that right? – jrz Aug 15 '15 at 23:16
0

I'm sure one of the Apache boffins will have a more elegant solution, but with my limited apache experience I would aim to achieve this using the vhosts module (Virtual Hosts). My experience in using this is limited to configuring Apache to act as a reverse proxy. A reverse Proxy typically receives and inspects incoming requests, it then generates a new request on behalf of the client to a different location as specified in the vhosts configuration. The criteria for the new request in this application is typically domain name, and hence, may assist you as you could make the URL www.mydomain... appear to be identical to mydomain...

First, Enable the Proxy Modules in httpd.conf.

LoadModule negotiation_module modules/mod_negotiation.so
LoadModule proxy_module modules/mod_proxy.so

Then include the vhost config via httpd.conf

Include conf/extra/httpd-vhosts.conf

In httpd-vhosts.conf enable named virtual hosts on port 80

NameVirtualHost *:80

Then create the entry for your site. Note that the newlocation URL is from the Proxies perspective. This could possibly just be localhost to simplify things.

<VirtualHost *:80> 
    ProxyPreserveHost On
    ProxyRequests off
    ServerName myalis.mydomain.com
    ProxyPass / http://newlocation.mydomain.com/
    ProxyPassReverse / http://newlocation.mydomain.com/
</VirtualHost>

You can use this method to also access files on other servers behind a firewall, allowing multiple servers to effectively share port 80 for web traffic from an outsiders perspective. You could also change the port in your request so a site hosted on another port appears to be on port 80 or otherwise.

Don't forget to backup files before changes, and restart Apache after configuration updates. More information about Apache as a reverse proxy can be found here:

Configure Apache as a Reverse Proxy

Apache Reverse Proxies

Élie
  • 1,285
  • 1
  • 12
  • 27
-2

Redirecting www to non-www - use instructions provided. Apply inside <Directory> in httpd.conf, if not using .htaccess. Requires ModRewrite.

Sergey
  • 1,181
  • 7
  • 18