Mobile app version of vmapp.org
Login or Join
Yeniel560

: Permanent Redirect (WWW to non WWW) This is my vhost conf and still permanent redirect isn't working. DocumentRoot /var/www/example ServerName example.com ServerAlias www.example.com <Directory "/var/www/example">

@Yeniel560

Posted in: #Redirects

This is my vhost conf and still permanent redirect isn't working.

DocumentRoot /var/www/example
ServerName example.com
ServerAlias example.com <Directory "/var/www/example">
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
RedirectPermanent www.example.com example.com

10.02% popularity Vote Up Vote Down


Login to follow query

More posts by @Yeniel560

2 Comments

Sorted by latest first Latest Oldest Best

 

@Ravi8258870

The first parameter of RedirectPermanent should be a path, not a full URL (e.g. RedirectPermanent /foo example.com/bar), so you won't quite be able to get what you're trying to do to work.

You should be able to do it if you split the www part into a separate vhost:

<VirtualHost *:80>
DocumentRoot /var/www/example
ServerName example.com
<Directory "/var/www/example">
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
</VirtualHost>

<VirtualHost *:80>
DocumentRoot /var/www/example
ServerName example.com RedirectPermanent / example.com </VirtualHost>


However, this will only redirect the homepage. If you want to redirect all example.com requests you'll want to use mod_rewrite as in closetnoc's example. You don't have to use htaccess files - those directives are perfectly valid in the directory block of a vhost:

ServerName example.com
ServerAlias example.com <Directory "/var/www/example">
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all

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


Also, if you are concerned about the performance of .htaccess files (which is negligible, but it exists), you probably don't want to use AllowOverride All, since that's what tells Apache to look for them.

10% popularity Vote Up Vote Down


 

@Ann8826881

I never try and do redirects on the configuration file.

I would suggest removing your redirect and creating an .htaccess file within the root of the web space with the following code:

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

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme