Mobile app version of vmapp.org
Login or Join
Michele947

: 301 Redirect from www to non-www without using VHOST I have my web application running on EC2 with Apache. I want to redirect www to non-www. On searching Google, I used this but it leads

@Michele947

Posted in: #301Redirect #AmazonEc2 #Apache #HttpdConf #NoWww

I have my web application running on EC2 with Apache. I want to redirect www to non-www. On searching Google, I used this but it leads to redirect loop:

<VirtualHost *:80>
ServerName domain.com Redirect 301 / domain.com/ </VirtualHost>


I am not using virtual hosts as the server only handles one domain. What can be wrong in above example and How can I do 301 redirect?

P.S. I am trying to avoid using .htaccess.

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Michele947

1 Comments

Sorted by latest first Latest Oldest Best

 

@Si4351233

You have to question some confusion, so I'll talk about ways to do a redirect from www to no-www:

1.Create two VirtualHost for two domains and use 301 redirect:

NameVirtualHost *:80
<VirtualHost *:80>
ServerName example.com
DocumentRoot "/path/to/site"
</VirtualHost>
<VirtualHost *:80>
ServerName example.com Redirect 301 / example.com/ </VirtualHost>


2.Create two VirtualHost for two domains and use .htaccess with redirect-rule:

<VirtualHost *:80>
ServerName example.com
DocumentRoot "/path/to/site1"
</VirtualHost>
<VirtualHost *:80>
ServerName example.com DocumentRoot "/path/to/site2"
</VirtualHost>


and create /path/to/site2/.htaccess with

Redirect 301 / example.com/

3.Create 1 VirtualHost and set redirect in common .htaccess:

<VirtualHost *:80>
ServerName example.com
ServerAlias example.com DocumentRoot "/path/to/site"
</VirtualHost>


create /path/to/site/.htaccess with

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ example.com/ [R=301,L]

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme