简体   繁体   English

为特定域重定向 301 htaccess

[英]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.我有 2 个域指向同一个文件夹/网站(multishops prestashop),我想从特定页面上的第一个域重定向一些页面并保留域。

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.但是我不能用简单的重定向 301 来做到这一点,因为我无法从请求 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.对不起我的英语,并提前感谢您的帮助。

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.除了@DusanBajic 在他对您的问题的评论中解释的内容之外,您还需要考虑重写规则中绝对路径和相对路径之间的区别。 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.在分布式配置文件(“.htaccess”)中实现时,规则模式与请求的 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.当规则在真正的 http 服务器的主机配置中实现时也是如此,其中模式与请求的 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.最好从 302 临时重定向开始,然后在确定一切设置正确后才将其更改为 301 永久重定向。 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).此实现将在 http 服务器主机配置或分布式配置文件(“.htaccess”文件)中类似地工作。 Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host.显然重写模块需要在http服务器中加载并在http主机中启用。 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.如果您使用分布式配置文件,您需要注意在主机配置中完全启用它的解释,并且它位于主机的DOCUMENT_ROOT文件夹中。

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").还有一个一般性的评论:你应该总是喜欢将这样的规则放在 http 服务器主机配置中,而不是使用分布式配置文件(“.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.那些分布式配置文件增加了复杂性,通常是意外行为的原因,难以调试,而且它们确实降低了 http 服务器的速度。 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).它们仅在您无法访问真正的 http 服务器主机配置(阅读:非常便宜的服务提供商)或坚持编写自己的规则的应用程序(这是一个明显的安全噩梦)的情况下作为最后一个选项提供。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM