alexandre bru
alexandre bru

Reputation: 103

Redirect 301 htaccess for specific domain

I try several things for my redirections but without working solution.

I have 2 domains pointing in the same folder/website (mulitshops prestashop) and I would like to redirect some page from the first domain on a specific page and keep domain.

My work :

Options +FollowSymLinks -MultiViews
RewriteEngine On

RewriteCond %{HTTP_HOST} ^domain1.local$ [NC]
RewriteRule ^/fr/blog/inspirations-c4$ https://domain1.local/fr/journal [R=301,L]
RewriteRule ^/fr/blog/inspirations-c3$ https://domain1.local/fr/journal [R=301,L]

RewriteCond %{HTTP_HOST} ^domain2.local$ [NC]
RewriteRule ^/fr/blog/inspirations-c4$ https://domain2.local/fr/test [R=301,L]
RewriteRule ^/fr/blog/inspirations-c3$ https://domain2.local/fr/test [R=301,L]

But I cant do this with simple Redirect 301 because I can't specify the domain from request URI. Beause the target page it's not the same according to the domain.

Sorry for my english and thank you in advance for your help.

Upvotes: 0

Views: 131

Answers (1)

arkascha
arkascha

Reputation: 42885

In addition to what @DusanBajic explained in his comment to your question you also need to consider the difference between absolut and relative path in rewriting rules. This is actually explicitly documented...

WHen implemented in distributed configuration files (".htaccess") the rule pattern is matched against the relative path of the requested URL. You however try to match it against an absolute path which will never match. So either change your patterns to use relative paths too, or, preferably, implement your rewriting rules such that they work in both cases. So also when the rules are implemented in the real http server's host configuration where the pattern is matched against the absolute path inside the requested URL. This appears confusing at first. But it does make total sense, once you think about it.

Options +FollowSymLinks -MultiViews

RewriteEngine On

RewriteCond %{HTTP_HOST} ^domain1\.local$ [NC]
RewriteRule ^/?fr/blog/inspirations-c4$ https://domain1.local/fr/journal [R=301,L]
RewriteCond %{HTTP_HOST} ^domain1\.local$ [NC]
RewriteRule ^/?fr/blog/inspirations-c3$ https://domain1.local/fr/journal [R=301,L]

RewriteCond %{HTTP_HOST} ^domain2\.local$ [NC]
RewriteRule ^/?fr/blog/inspirations-c4$ https://domain2.local/fr/test [R=301,L]
RewriteCond %{HTTP_HOST} ^domain2\.local$ [NC]
RewriteRule ^/?fr/blog/inspirations-c3$ https://domain2.local/fr/test [R=301,L]

It is a good idea to start out with a 302 temporary redirection and only change that to a 301 permanent redirection later, once you are certain everything is correctly set up. That prevents caching issues while trying things out..

This implementation will work likewise in the http servers host configuration or inside a distributed configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a distributed configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.

And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using distributed configuration files (".htaccess"). Those distributed configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).

Upvotes: 1

Related Questions