Forum Moderators: phranque

Message Too Old, No Replies

Issues with htaccess redirects after creating subdomain

Involved are two Wordpress installations

         

JohnM1963

9:05 am on Mar 27, 2015 (gmt 0)

10+ Year Member



I currently have a main domain (example.nl) and a subdomain (zakelijk.example.nl) hosted on the same machine. The WP installation for the main domain in in root, the WP for the subdomain is in a folder 'zakelijk'.

I had redirects like
Redirect 301 /trainingen /workshops-en-trainingen/

in the default htaccess file before I added the subdomain. These redirects worked perfectly well. After creating the subdomain however, I noticed that the redirects were also picked up from zakelijk.example.nl. As this for me is undesireable, I changed the complete code to this (i've numbered the rewrite rules):

[size=2]# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
# custom redirects
Options +FollowSymLinks -MultiViews
#
RewriteCond %{HTTP_HOST} !^www\.example\.nl$ [NC]
RewriteRule .? –[S=5]
#1
RewriteRule ^/workshop\-enneagram/(.*)$ /workshops\-en\-trainingen/workshop\-enneagram/$1 [R=301,NC,L]
#2
RewriteRule ^/workshop/(.*)$ /workshops\-en\-trainingen/$1 [R=301,NC,L]
#3
RewriteRule ^/workshops/(.*)$ /workshops\-en\-trainingen/$1 [R=301,NC,L]
#4
RewriteRule ^/training/(.*)$ /workshops\-en\-trainingen/$1 [R=301,NC,L]
#5
RewriteRule ^/trainingen/(.*)$ /workshops\-en\-trainingen/$1 [R=301,NC,L]
# end of custom redirects
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress[/size]


The filter for the www-domain now works. Unfortunately the redirects behave in a totally odd way. Rules 1 and 3 work as expected. Rule 2 redirects to the wrong adress and rules 4 and 5 do not redirect and then give a 404.

I've been looking all over the place, but cannot figure out what is going on that's causing this. Please bear with me: I am definitely not an expert in these things :-)

Any help is highly appreciated.

John

PS: I just discovered something strange (at least to me)

If I call www.example.nl/trainingen/?p=3098767356 then all of a sudden the redirect works and takes me to http://www.example.nl/workshops-en-trainingen/

So any random query string behind the url makes all redirects work. Does this in anyway clarify the issue? Without it they stil do not work.

[edited by: phranque at 12:30 pm (utc) on Mar 27, 2015]
[edit reason] Please Use example.com [webmasterworld.com] [/edit]

phranque

12:37 pm on Mar 27, 2015 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



welcome to WebmasterWorld, JohnM1963!


there shouldn't be any need to escape the dashes with backslashes in the targets of RewriteRules.

those targets should specify the canonical protocol and hostname.

i would not bother using the [NC] flag in those RewriteRules.

not2easy

6:55 pm on Mar 27, 2015 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



One issue that can affect your rules is that they are placed inside the WordPess envelope. The htaccess between
# BEGIN WordPress

and
# END WordPress

should not be edited for domain rewrites. Those should be done separately in your htaccess file and come before the WP envelope. WP updates can and do overwrite the rules inside the WP envelope.

These rules:
# custom redirects
Options +FollowSymLinks -MultiViews
#
RewriteCond %{HTTP_HOST} !^www\.example\.nl$ [NC]
RewriteRule .? –[S=5]
#1
RewriteRule ^/workshop\-enneagram/(.*)$ /workshops\-en\-trainingen/workshop\-enneagram/$1 [R=301,NC,L]
#2
RewriteRule ^/workshop/(.*)$ /workshops\-en\-trainingen/$1 [R=301,NC,L]
#3
RewriteRule ^/workshops/(.*)$ /workshops\-en\-trainingen/$1 [R=301,NC,L]
#4
RewriteRule ^/training/(.*)$ /workshops\-en\-trainingen/$1 [R=301,NC,L]
#5
RewriteRule ^/trainingen/(.*)$ /workshops\-en\-trainingen/$1 [R=301,NC,L]
# end of custom redirects

should be in your htaccess file before the start of the WP envelope.

As phranque mentioned, the target of your rule does not need to be \ escaped and the NC is not normally needed unless you know that you have links in various case formats.

Just curious: do the folder names actually have a hyphen before each folder name?

lucy24

9:22 pm on Mar 27, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



the WP for the subdomain is in a folder 'zakelijk'.

In what follows, I will assume you meant that the subdomain's folder is inside the root folder, not side by side.
I had redirects like
Redirect 301 /trainingen /workshops-en-trainingen/
in the default htaccess file before I added the subdomain. These redirects worked perfectly well.

You surprise me. Even if you only had one WP installation, that's one too many for Redirect (or RedirectMatch). mod_alias (Redirect by that name) and mod_rewrite (RewriteRule, the core of any WP site) do not play nice together. So make sure you get rid of every last occurrence of Redirect-by-that-name in all htaccess files everywhere.

Two WP installations means two htaccess files, each containing mod_rewrite. Since RewriteRules aren't inherited in the usual way, and even "RewriteOptions inherit" doesn't work the way you'd expect it to, this means you will need to duplicate. Any RewriteRule that's meant to apply to all requests-- whether in the subdomain or the primary domain-- will need to be repeated in htaccess.

JohnM1963

3:11 pm on Mar 28, 2015 (gmt 0)

10+ Year Member



Thanks all for your replies. In the meantime I got things solved (so it seems). I had multiple issues to solve so it turned out:
1. rewrite domain.nl to www.domain.nl (otherwise 500 error)
2. add trailing slashes to url's without trailing slashes (I had trailing slashes in the rewrite rules but then url's without them would not be redirected. So now I add a slash if it's missing and then do the rewrite on the url with the trailing slash)
3. then get redirects working on main domain

As to the question on the hyphen: it is part of some folder names. The escaping I did 'just in case' and was part of a lot of trial and error fixing attempts. Thanks for the tip on the wordpress envelope. I'll fix that.

John

lucy24

7:49 pm on Mar 28, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



1. rewrite domain.nl to www.domain.nl (otherwise 500 error)

I hope you meant redirect. The 500 error alarms me. Was it some kind of infinite loop?