简体   繁体   English

将所有子 url 重定向到主 url

[英]redirect all sub url to primary url

I have a wordpress site running on apache.我有一个在 apache 上运行的 wordpress 站点。 I need to redirect domain.com/examplepage/* to domain.com/examplepage我需要将domain.com/examplepage/*重定向到domain.com/examplepage

so as an example举个例子

domain.com/examplepage/randomstring/randomstring world go to domain.com/examplepage domain.com/examplepage/randomstring/randomstring世界转到domain.com/examplepage

I've tried to google how to do this but cant find a way.我试图谷歌如何做到这一点,但找不到办法。 I'm sure it's because I just don't know how to search for the correct thing.我确定这是因为我只是不知道如何搜索正确的东西。 I'm willing to use wordpress plugin, .htaccess, or apache config.我愿意使用 wordpress 插件、.htaccess 或 apache 配置。 Whatever works.什么都行。

Well, you can see many, many answers to this here on SO.好吧,你可以在 SO 上看到很多很多答案。 Did you check the "Related" section on the right hand side here?您是否检查了此处右侧的“相关”部分?

Anyway, here is what you are probably looking for:无论如何,这就是您可能正在寻找的内容:

RewriteEngine on
RewriteRule ^/?examplepage/.+ /examplepage [END]

The above implements an internal rewrite.以上实现了内部重写。 In case you really want an external redirection instead this would be the variant:如果您真的想要外部重定向,这将是变体:

RewriteEngine on
RewriteRule ^/?examplepage/.+ /examplepage [R=301]

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, certainly you will need to add another rewriting condition to break an endless rewriting loop.您可以尝试升级或使用旧的[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