Warm tip: This article is reproduced from stackoverflow.com, please click
.htaccess apache redirect url-rewriting multisite

.htaccess redirects on masked domains, only written to take effect when specific domains are in use

发布于 2020-04-07 10:19:35

I have a WordPress multisite set up on an Apache server. For SEO reasons, I'm trying to shift as many possible JS redirects to HTTP 301s in the .htaccess file. Because the domains on this multisite are masked, all of these domains share one top-level directory and one .htaccess file. Because of this, I am unable to make every redirect a simple

Redirect 301 /example/ https://example.com/example/ 

Example being, if we have one service that closes a location in Austin TX, another service that still has one open in Austin TX, and both URLs are set up as "example.com/austin/", I can't have a catch-all redirect sending "/austin/" to one place. This creates a ton of potential conflicts.


So I'm trying to create redirects that ONLY take effect when a specific masked domain is in use. Below is the code I'm using, but it's not working, notice I'm making sure that both trailing slashes and non-trailing slashes are covered:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com [nc]
RewriteRule ^old1$ https://example.com/new1/ [R=301,NC,L]
RewriteRule ^old1/$ https://example.com/new1/ [R=301,NC,L]
RewriteRule ^old12$ https://example.com/new12/ [R=301,NC,L]
RewriteRule ^old12/$ https://example.com/new12/ [R=301,NC,L]
#...

What's going wrong?

Questioner
AustinLovelace13
Viewed
87
AustinLovelace13 2020-02-04 01:26

I figured it out!

I had a rule in there to force trailing slashes.

RewriteCond %{REQUEST_URI} /+[^\.]+$
RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]

Because it was above all the redirects, it voided them. I moved that rule below them and it fixed it.