3

I'm trying to redirect domain.com to domain.se/en, but it just won't work. It's like another rewrite or redirect is happening to domain.se/sv before I get the chance. I've tried multiple VirtualHosts configs and .htaccess variants without success. domain.com uses an A record that points to the same IP as domain.se (main domain) does. I suspect the WordPress plugin Polylang might be rewriting the URL or causing a redirect before I get the chance.

My .htacces currently looks like this:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} .*domain.com.*$
RewriteRule ^(.*)$ http://domain.se/en [L]
</IfModule>
# 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

and my VirtualHosts config looks like this: (Note that both .no and .fi also wrongly redirects to .se/sv)

NameVirtualHost *:80
<VirtualHost *:80>
        ServerName domain.com
        ServerAlias www.domain.com
        RedirectPermanent / http://domain.se/en
</VirtualHost>

<VirtualHost *:80>
        ServerName domain.fi
        ServerAlias www.domain.fi
        RedirectPermanent / http://domain.se/fi
</VirtualHost>

<VirtualHost *:80>
        ServerAdmin admin@domain.com
        ServerName domain.se
        ServerAlias www.domain.se
        ServerAlias domain.no
        DocumentRoot /var/www/html
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

        <Directory /var/www/html>
                AllowOverride All
                Order allow,deny
                allow from all
        </Directory>
</VirtualHost>

This is what it looks like in Chrome Dev tools Network section when I try to go to domain.com:

Chrome Dev Tools - Network Tab - Showing redirect

I flushed DNS cache locally and in Chrome before visiting the domain.

MrWhite
  • 12,647
  • 4
  • 29
  • 41
  • "My .htacces currently" - I assume that's just a typo? It should be `.htaccess`. Do you have any additional `.htaccess` files in subdirectories? Are `/en` or `/sv` physical subdirectories? Note, you should include the `R` flag in your `RewriteRule` (although it will implicitly trigger a 302 in this instance since you have specified an absolute URL.) – MrWhite Dec 23 '16 at 17:06
  • 1
    Also, try adding a `R=307` in your `RewriteRule` directive - do you see the 307 status in the network traffic? – MrWhite Dec 23 '16 at 17:36
  • 1. Just a typo here 2. Thank you! Redirect 307 / http://domain.se/en worked! If you add your comment as an answer I'll "accept" it :) – Jesper Johansson Dec 27 '16 at 07:26
  • The `R=307` is not a solution - that was just to help with debugging. Simply changing that would not actually change anything. I also didn't suggest changing that redirect to a mod_alias `Redirect` - which would appear to do something different to what you require (if we are talking about the same thing)? (In fact, mixing mod_alias `Redirect` and `RewriteRule` is not recommended.) It's not clear what is going on here. My guess is that "something else" has changed, or there was some caching issue (although you say you cleared all _local_ caches)? – MrWhite Dec 27 '16 at 17:25
  • If you are adamant that your changes solved this then please document your solution as an answer, which you can later accept. Much appreciated. :) – MrWhite Dec 27 '16 at 17:27

1 Answers1

0

I had the same problem. WordPress seem to be the culprit in this header. I do not know your project structure, so I would suggest either two approach to troubleshoot the issue:

Header always edit Location in your VirtualHost configuration, requires mod_headers. This will always edit the header after any modification made by application, i.e., Zend, WordPress, Spring, etc.

wp_redirect, from answer, should help with your troubleshooting without touching server configuration. This is probably not the elegant solution to your question but this is a function that you could use to debug application.

jmsweb
  • 101
  • 5
  • But unless WordPress is generating additional `.htaccess` files(?!) then the above Apache directives should override anything that WordPress (ie. PHP) would be able to do?! WordPress shouldn't even be getting a look in? – MrWhite Dec 23 '16 at 17:12
  • Some of my applications integrate with `Zend`, `PHP`, and `WordPress`, and runs on `httpd` with `mysqld`. Sometime I need to update `siteurl` in WordPress database to direct for correct domain. I'm not 100% knowledgeable about Zend or WordPress but this issue occurs _after_ integration. – jmsweb Dec 23 '16 at 17:48