0

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">
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
RedirectPermanent http://www.example.com http://example.com

2 Answers 2

3

The first parameter of RedirectPermanent should be a path, not a full URL (e.g. RedirectPermanent /foo http://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 www.example.com
    RedirectPermanent / http://example.com
</VirtualHost>

However, this will only redirect the homepage. If you want to redirect all www.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 www.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 ^(.*)$ http://example.com/$1 [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.

3
  • BTW- the .htaccess file is cached these days and only checked for a time stamp that does not match the cache. So for the most part, the .htaccess is not touched if it remains static short of checking the file index last modified date/time periodically. Even this can be cached by the file I/O sub-system depending upon request patterns. So "negligible" can actually be "no impact" under normal circumstances.
    – closetnoc
    Commented May 6, 2015 at 2:24
  • 1
    BTW- A much more detailed answer- one up-vote just for that!!
    – closetnoc
    Commented May 6, 2015 at 2:25
  • "this will only redirect the homepage" - Actually, the RedirectPermanent directive will redirect everything to the corresponding URL at example.com (it's prefix matching and everything after the match is passed through). No need for mod_rewrite here.
    – MrWhite
    Commented Apr 16, 2016 at 15:01
0

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 ^(.*)$ http://example.com/$1 [L,R=301]
4
  • But .htccess code is interpreted for each and every HTTP request to the server. I think CPU load is much less if the code is placed in conf. (Oh btw hello :D you helped me before and we had a nice chat) Commented May 6, 2015 at 1:01
  • Hello!! How are you?? It is a bit of a misnomer that resources are used to read the .htaccess file- it is, but with systems so fast these days, there is no real effect. That was the mantra back in the 486 days and still, I used .htaccess without issue. If you saw the code in my .htaccess, you would cringe! Long and involved. My processes are changing so it will get shorter, but still it will be miles long compared to others. There is no noticeable hit to the performance of the system even with as much as 78,000 page views a day.
    – closetnoc
    Commented May 6, 2015 at 1:14
  • I'm fine thank you :) Well if theres no noticeable difference to the performance i'll use the htaccess code then. By the way, yesterday some russians tried to hack my server but they failed miserably, it's so funny to watch the logs sometimes :D Commented May 6, 2015 at 1:42
  • I have been through it. The Chinese performed a heavy DOS attack on my servers for almost a year... but I was able to stop them up-stream since I owned that router/firewall. Congratulations on surviving the attack!!
    – closetnoc
    Commented May 6, 2015 at 2:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.