【问题标题】:htaccess: If the requested URL doesn't contain a specific string, redirect user to different domain while maintaining the slughtaccess:如果请求的 URL 不包含特定字符串,则将用户重定向到不同的域,同时保持 slug
【发布时间】:2021-05-10 04:40:48
【问题描述】:

我在这里浏览了很多不同的帖子,但没有任何运气。

我有两个网站(例如):exampledomain.com 和 mb.exampledomain.com

如果用户访问 mb.exampledomain.com 并且 URL 包含字符串“retreat”,那么我想将他们重定向到 exampledomain.com 而维护蛞蝓。

所以:

mb.exampledomain.com/retreat(或)mb.exampledomain.com/retreat_sunday 应该可供用户正常访问

mb.exampledomain.com/(或)mb.exampledomain.com/about-us 应分别重定向到 exampledomain.com/(或)exampledomain.com/about-us。

我需要通过在目录根目录的 .htaccess 文件中使用规则来实现这一点。

此外,htaccess 规则不应影响 mb.exampledomain.com/retreat 页面上资产文件的可访问性。

感谢您提供的任何帮助!

【问题讨论】:

    标签: wordpress apache .htaccess mod-rewrite


    【解决方案1】:

    听起来很简单:

    RewriteEngine on
    RewriteCond %{REQUEST_URI} !^/retreat
    RewriteRule ^ https://exampledomain.com%{REQUEST_URI} [QSA,R=301,END]
    

    这会捕获所有对 URL 的请求,而不是 以文字字符串“retreat”开始。这就是您的示例所展示的。如果您只想检查字符串本身,而不管它出现在路径中的哪个位置,那么应该按照您的要求进行:

    RewriteEngine on
    RewriteCond %{REQUEST_URI} !retreat
    RewriteRule ^ https://exampledomain.com%{REQUEST_URI} [QSA,R=301,END]
    

    最好先从 302 临时重定向开始,然后在确定一切按预期工作后才将其更改为 301 永久重定向。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-08
      • 1970-01-01
      • 2011-03-14
      相关资源
      最近更新 更多