0

According to this question, Apache should not simply redirect from other ServerAlias domains to the ServerName domain, but in my case, it does.

For example, when I access www.yannbane.net, which is one of my domains point to my server's IP, it redirects me to yannbane.com. When I access www.yannbane.com, it redirects to yannbane.com as well.

This is the behavior that I actually want, but I don't understand what's happening here!

Here's my site's config:

<VirtualHost *:80>
    ServerName yannbane.com
    ServerAlias yannbane.net yannbane.org www.yannbane.net www.yannbane.org www.yannbane.com
    DocumentRoot "/var/www/yannbane.com/wordpress"
    ErrorLog "/var/log/yannbane.com/wordpress/error.log"
</VirtualHost>

Here's apache2.conf:

ServerName localhost

Mutex file:${APACHE_LOCK_DIR} default    
PidFile ${APACHE_PID_FILE}

Timeout 300

KeepAlive On

MaxKeepAliveRequests 100

KeepAliveTimeout 5


User ${APACHE_RUN_USER}
Group ${APACHE_RUN_GROUP}

HostnameLookups Off

ErrorLog ${APACHE_LOG_DIR}/error.log

LogLevel warn

# Include module configuration:
IncludeOptional mods-enabled/*.load
IncludeOptional mods-enabled/*.conf

# Include list of ports to listen on
Include ports.conf

<Directory />
    Options FollowSymLinks
    AllowOverride None
    Require all denied
</Directory>

<Directory /usr/share>
    AllowOverride None
    Require all granted
</Directory>

<Directory /var/www/>
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>

AccessFileName .htaccess

<FilesMatch "^\.ht">
    Require all denied
</FilesMatch>


LogFormat "%v:%p %h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" vhost_combined
LogFormat "%h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %O" common
LogFormat "%{Referer}i -> %U" referer
LogFormat "%{User-agent}i" agent

IncludeOptional conf-enabled/*.conf

IncludeOptional sites-enabled/*.conf

Here's my .htaccess in my WordPress directory (which is the document root for this virtual host):

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

Can anyone tell me why are these redirects happening? And what's the appropriate way of achieving such behavior?

Community
  • 1
  • 1
corazza
  • 31,222
  • 37
  • 115
  • 186
  • 1
    You may have extra related configuration in `.htaccess` files as you have an `AllowOverride All` from `/var/www` – regilero Oct 28 '13 at 11:37
  • Probably, but how exactly would I investigate that? In either case I've added my WordPress `.htaccess` file contents to the question now... – corazza Oct 28 '13 at 21:06
  • 2
    Wordpress does this, nothing to do with the Apache configuration. As I don't run WP, poke around in it's configuration and see if you can disable that. (You could even test this: make a static test.html page, and I'll bet good money that doesn't get redirected as it is not run through WP's framework). – Wrikken Oct 28 '13 at 21:13
  • @Wrikken - why don't you post that as an answer so I can accept it? I've just checked it and yes, WordPress is performing redirects. I can change this in the main settings section, so it could theoretically point to `www.yannbane.com`. – corazza Oct 29 '13 at 17:41

1 Answers1

0

You can add below code in .htaccess to redirect the domain with www

 Options +FollowSymLinks
 RewriteEngine on
 RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L] 

Actually this will help to redirect on new domain, here we can use www.yannbane.com instead of www.newdomain,

http://techietent.blogspot.in/2013/03/url-redirection-using-htaccess.html

Ranjithkumar T
  • 1,886
  • 16
  • 21