3

I have to manually redirect few URLs in my website1 to website2.

Below is my code in the .htaccess file of website1

Redirect 301 /post1/ https://www.website2.com/post1

When I enter https://www.website1.com/post1/ in the browser it's being redirected to https://www.website2.com/post1 successfully, as expected.

But, When I enter https://www.website1.com/post1 in the browser it's being redirected to https://www.website2.compost1, the slash is missing after https://www.website2.com

What could be done to solve this?

Anirudh
  • 2,767
  • 5
  • 69
  • 119

3 Answers3

5

You can do this in a single rule using RedirectMatch that uses a regex to make trailing slash optional as this:

RedirectMatch 301 ^/(post1)/?$ https://www.website2.com/$1

Added benefit is avoiding repeat of post1 in source and target by using a capture group in source and using back-reference $1 in target.

anubhava
  • 761,203
  • 64
  • 569
  • 643
2

Remove the trailing / from the Redirect.

Redirect 301 /post1 https://www.website2.com/post1

This redirect then works for both versions of the URL. See testing link here.

Joe
  • 4,877
  • 5
  • 30
  • 51
1

Using 2 Redirect URLs in this particular order solved it.

Redirect 301 /post1 https://www.website2.com/post1

Redirect 301 /post1/ https://www.website2.com/post1
Anirudh
  • 2,767
  • 5
  • 69
  • 119
  • 1
    You shouldn't need two to accomplish this. The one with the trailing slash removed should've been sufficient. You're simply adding an extra redirect for your server to run prior to loading. – Joe Jul 19 '18 at 12:51
  • @Joe I tried, but it didn't work. That's the reason I had to add both. – Anirudh Jul 19 '18 at 17:13