Fearless Mode
Fearless Mode

Reputation: 241

Redirect Domain and Slug to different Domain with using same Slug

I'm not sure how to word this in simple form to return the appropriate search results, so I've created a Stack.

I have two domains.

Domain1.com

Domain2.com

I want all instances of Domain2.com to redirect to Domain1.com automatically.

If a slug path is provided for Domain2.com I want that slug path carried over to be detected by Domain1.com.

Then, I want Domain1.com to detect anything coming from Domain2.com and to redirect accordingly to the matching slug path on Domain1.com.

Example:

Domain2.com/this-slug

automatically redirects to

Domain1.com/this-slug

... because Domain1.com understands what to do exactly with Domain2.com and any slug if passed along with it.

I would prefer (I think) this to be done via .htaccess but I am open to other methods. In the next year I expect to move to NGINX so I'm not sure how that would work which is why I'm open to other methods to carry over at that time.

I think the difference from search results I did find is that I want this done automatically and not to enter manually the slug path for each one needed. I'm essentially using Domain2.com as Share URL to Domain1.com.

Upvotes: 1

Views: 3155

Answers (1)

Eduardo Escobar
Eduardo Escobar

Reputation: 3389

If you want to redirect domain2.com/this-slug to domain1.com/this-slug, add these rewrite rules to domain2.com's htaccess file:

RewriteEngine On

# First condition, requested resource is not an existing file in domain2.com (this condition can be removed if needed)
RewriteCond %{REQUEST_FILENAME} !-f

# Second condition, requested resource is not an existing directory file in domain2.com (this condition can be removed if needed)
RewriteCond %{REQUEST_FILENAME} !-d

# Third condition, slug must exist
RewriteCond %{REQUEST_URI} !^/?$

# Do the redirection
RewriteRule ^(.*)$ http://domain1.com/$1 [R=301,QSA,L]

Please note that with this rewrite rule domain2.com/something/this-slug would redirect to domain1.com/something/this-slug, if you don't want to redirect if there are intermediate slashes within the request URI, use this as third condition instead:

RewriteCond %{REQUEST_URI} ^/?[^/]+$

Upvotes: 1

Related Questions