3

I'm trying to redirect individual URLs on http://olddomain.tld (retired WordPress site) to individual URLs on https://newdomain.tld (new WordPress site) using .htaccess.

Redirecting the home page works fine with Redirect 301 / https://newdomain.tld (so I know I didn't forget to turn on Rewrite engine, or anything like that).

None of the other redirects works correctly, however. Example: I write Redirect 301 /page/ https://newdomain.tld/page/in .htaccess, and then http://olddomain.tld/page/ redirects to https://newdomain.tldpage. That is, the redirect is present, but it drops the trailing slash in every case.

What could be causing this?

I tried all kinds of increasingly exotic variations of the olddomain.tld .htaccess, before finally getting the idea to see if trailing slashes are somehow getting dropped by newdomain.tld's .htaccess. Could something in here be doing it?

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

ps. It's not this, this, this, this, this or this.

  • "https://newdomain.tldpage" - presumably that's just a typo, there is a slash after the domain name? – MrWhite Jan 23 '17 at 20:49
  • It might be worth including your .htaccess from olddomain.tld. – MrWhite Jan 23 '17 at 21:36
1

If the trailing slash is being "dropped" then there maybe something in the WordPress config at the destination site that is doing this. Check the network/HTTP traffic... at what point do you see the second redirect? What HTTP status is the second redirect?

If you type the new URL directly is the trailing slash still dropped?

However...

I write Redirect 301 /page/ https://newdomain.tld/page/ in .htaccess ...

The Redirect directive is a mod_alias directive. This has nothing to do with RewriteEngine, which is part of mod_rewrite. Different Apache modules work independently and you should avoid mixing redirects from both modules - you can get unexpected conflicts (since mod_rewrite runs first, regardless of the apparent order of these directives in .htaccess).

Since WordPress already uses mod_rewrite, you should change these redirects to use mod_rewrite instead and these should go before the existing WordPress directives. So, for example:

# Home page (ie. "/")
RewriteRule ^$ https://newdomain.tld/ [R=301,L]

# Other page (ie. "/page/")
RewriteRule ^page/$ https://newdomain.tld/page/ [R=301,L]

Note that there is no slash prefix on the RewriteRule pattern (as there is with mod_alias Redirect).

Could something in here be doing it?

That is simply the WordPress front controller - no that can't drop the trailing slash. But presumably there is some URL canonicalisation in the WordPress config itself?

1

There is nothing wrong in the WordPress provided .htaccess code, as it only does internal rewrites. No external redirect is being done in the newdomain.tld's .htaccess code as you've provided:

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

The ending slash is either being dropped by WordPress core, depending on the rewrite rules you (or your plugins) have set in WordPress or by some other server configuration.

To check if it is WordPress that is doing it, go to:

WordPress Admin Panel => Settings => Permalinks

and check the Permalink Settings. Then in Common Settings, check the ending of the URL structure. Most likely you don't have the ending slash (/) there.

So, if you are using Custom Structure like this:

https://newdomain.tld/%postname%

Make it as:

https://newdomain.tld/%postname%/

That should fix your problem. If it doesn't, it means the trailing slash is coming from somewhere else. If you have multiple plugins, try by disabling them & then enable one by one to check who is causing it.

Note: while testing URL redirects, always make sure you clear browser cache.

If none of it works, then your problem is coming from some other server configuration.

  • 1
    "The ending slash is either being added by..." / "...the trailing slash is coming from somewhere else." - the trailing slash is being "dropped", not added (as you also describe in other parts of your answer). – MrWhite Jan 24 '17 at 13:14
  • Ah! I Edited and missed the dropped part, thanks :) – Fayaz Jan 24 '17 at 13:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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