简体   繁体   English

301 url 在 Apache 服务器中重定向 .htaccess

[英]301 url redirect .htaccess in Apache server

How can i direct the search engines from one domain to other domain for better SEO optimization.我如何将搜索引擎从一个域引导到另一个域以获得更好的 SEO 优化。 I want to make 301 redirect from domain.uk to language directory of another domain domain.com/gr How can to change last line code?我想让 301 从 domain.uk 重定向到另一个域 domain.com/gr 的语言目录如何更改最后一行代码? Thanks!谢谢!

RewriteEngine on
RewriteCond %{HTTP_HOST} ^example-old\.uk$ [NC]
RewriteRule ^(.*)$ http://example-new.com/gr [R=301,L]
 RewriteCond %{HTTP_HOST} ^example-old\.uk$ [NC] RewriteRule ^(.*)$ http://example-new.com/gr [R=301,L]

You've not actually stated the problem you are having.您实际上并没有说明您遇到的问题。 However, if you want to redirect to the same URL-path, but with a /gr/ path segment prefix (language code) then you are missing a backreference to the captured URL path (otherwise there's no reason to have the capturing group in the RewriteRule pattern to begin with).但是,如果您想重定向到相同的 URL 路径,但使用/gr/路径段前缀(语言代码),那么您将缺少对捕获的 URL 路径的反向引用(否则没有理由在RewriteRule模式开始)。

For example:例如:

RewriteRule (.*) http://example-new.com/gr/$1 [R=301,L]

The $1 backreference contains the value captured by the preceding (.*) pattern. $1反向引用包含由前面的(.*)模式捕获的值。

I assume that is what you are looking for:我认为这就是您要寻找的东西:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^example-old\.uk$ [NC]
RewriteRule ^ http://example-new.com/gr%{REQUEST_URI} [R=301,END]

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...这可以防止在尝试时出现缓存问题......

In case you receive an internal server error (http status 500) using the rule above then chances are that you operate a very old version of the apache http server.如果您使用上述规则收到内部服务器错误(http 状态 500),那么您很可能操作的是非常旧版本的 apache http 服务器。 You will see a definite hint to an unsupported [END] flag in your http servers error log file in that case.在这种情况下,您将在 http 服务器错误日志文件中看到不支持的[END]标志的明确提示。 You can either try to upgrade or use the older [L] flag, it probably will work the same in this situation, though that depends a bit on your setup.您可以尝试升级或使用旧的[L]标志,在这种情况下它可能会起作用,尽管这取决于您的设置。

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