使用htaccess的简单301重定向

编程入门 行业动态 更新时间:2024-10-13 20:15:03
本文介绍了使用htaccess的简单301重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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

#Redirects RewriteEngine On RewriteRule tchibo.academyofsports.promo/ 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 ^/?(.*)$ 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 ^ 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$ www.academyofsports.de/Datenschutz [L,R=301] RewriteCondition %{HTTP_HOST} ^tchibo\.academyofsports\.promo$ [NC] RewriteRule ^/?(.*)$ 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重定向

本文发布于:2023-10-31 13:42:40,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1546255.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:重定向   简单   htaccess

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!