使用htaccess的简单301重定向 [英] Simple 301 redirect with htaccess

查看:125
本文介绍了使用htaccess的简单301重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从不再活动的页面重定向URL. URL https://tchibo.academyofsports.promo/以及所有子页面都应重定向到该URL http://www.academyofsports.de/.这是我到目前为止(.htaccess)的内容:

I want to redirect a URL from a page that is no longer active. The URL https://tchibo.academyofsports.promo/ as well as all subpages should be redirected to the URL http://www.academyofsports.de/. This is what I have so far (.htaccess):

#Redirects

RewriteEngine On
RewriteRule https://tchibo.academyofsports.promo/ http://www.academyofsports.de/ [L,R=301]

不幸的是,它不能以这种方式工作.我在做什么错了?

Unfortunately it does not work in this way. What am I doing wrong?

推荐答案

这可能是您正在寻找的:

This one probably is what you are looking for:

RewriteEngine On
RewriteCondition %{HTTP_HOST} ^tchibo\.academyofsports\.promo$ [NC]
RewriteRule ^/?(.*)$ http://www.academyofsports.de/$1 [L,R=301]

原因是RewriteRule仅对请求的URL的 path 组件起作用,它看不到主机名.上面的规则在http服务器主机配置和动态配置文件中也应同样起作用.

Reason is that a RewriteRule only operates on the path component of the requested URL, it cannot see the host name. The above rule should work likewise in the http servers host configuration and dynamic configuration files.

如果您对最初请求的路径不感兴趣,但想将所有人重定向到入口点,则可以将以上内容简化为:

If you are not interested in the path that got requested originally but want to redirect everyone to the entry point, then you can simplify the above to:

RewriteEngine On
RewriteCondition %{HTTP_HOST} ^tchibo\.academyofsports\.promo$ [NC]
RewriteRule ^ http://www.academyofsports.de/ [L,R=301]

和一般提示:您应该始终喜欢将此类规则放置在http服务器(虚拟)主机配置中,而不是使用动态配置文件(.htaccess样式文件).众所周知,这些文件容易出错,难以调试,并且确实降低了服务器的速度.仅当您无法控制主机配置(阅读:真正便宜的托管服务提供商)或者您的应用程序依赖于编写自己的重写规则(这显然是安全的噩梦)时,才提供它们作为最后的选择. ).

And a general hint: you should always prefer to place such rules inside the http servers (virtual) host configuration instead of using dynamic configuration files (.htaccess style files). Those files are notoriously error prone, hard to debug and they really slow down the server. They are only provided as a last option for situations where you do not have control over the host configuration (read: really cheap hosting service providers) or if you have an application that relies on writing its own rewrite rules (which is an obvious security nightmare).

在下面对您的评论做出回应,我想添加一下:

Reacting to your comment below I want to add this:

要实现特定的映射,您还可以添加例外规则:

To implement specific mappings you can also add exception rules:

RewriteEngine On

RewriteCondition %{HTTP_HOST} ^tchibo\.academyofsports\.promo$ [NC]
RewriteRule ^/?agb\.html$ http://www.academyofsports.de/Datenschutz [L,R=301]

RewriteCondition %{HTTP_HOST} ^tchibo\.academyofsports\.promo$ [NC]
RewriteRule ^/?(.*)$ http://www.academyofsports.de/$1 [L,R=301]

问题是由于重写模块的内部逻辑,您必须为每个规则重复条件.有一个或两个简单的解决方法可以解决此问题,但是事情很难以这种方式快速维护.如果您需要指定多个此类例外规则,则 RewriteMap 是要走的路.

The issue is that you have to repeat the condition for each single rule due to the internal logic of the rewrite module. There are one or two crude hacks to get around this, but things get hard to maintain very fast that way. If you need to specify multiple such exception rules a RewriteMap is the way to go.

这篇关于使用htaccess的简单301重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆