开发者

Redirect non www to www using httpd.conf

开发者 https://www.devze.com 2023-02-14 13:23 出处:网络
Is it possible to redirect from the non www 开发者_开发问答version to my website to the www version of the website using httpd.conf ?Two options:

Is it possible to redirect from the non www 开发者_开发问答version to my website to the www version of the website using httpd.conf ?


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.


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号