【问题标题】:URL Redirect, htaccess. Capture and insert after string if string is not foundURL 重定向,htaccess。如果找不到字符串,则在字符串后捕获并插入
【发布时间】:2021-01-13 11:39:23
【问题描述】:

我希望就以下重定向的 htaccess 解决方案获得一些帮助:

案例 1) 用户输入 domain.com 它应该重定向到 domain.com/nl

案例 2)如果用户输入 domain.com/nl 或 domain.com/fr 它不应该重定向

案例 3)用户输入 domain.com/some-path 它应该捕获 url 路径并在其前面粘贴 /nl,domain.com/nl/some-path

到目前为止,我已经尝试了以下规则:

Redirect 301 ^((?!(?:)nl($)|(?:)fr($)).)*$ domain.com/nl/$1

但是在案例 3 中,只选择了路径的最后一个字符,而不是整个 'some-path'。

使用多个规则也可以,但是可以在单个规则中完成吗?

【问题讨论】:

  • 能否请您在问题中提及您要从哪个 URL 重写/重定向到哪个 URL(仅示例 URL 可以),以便更好地理解您的问题。也请分享您的 htaccess 文件以便更好地理解,谢谢。

标签: apache .htaccess redirect mod-rewrite url-rewriting


【解决方案1】:

可以像这样在单个规则中完成:

RewriteEngine On

RewriteCond %{THE_REQUEST} !\s/+(?:nl|fr)[/?\s] [NC]
RewriteRule ^ /nl%{REQUEST_URI} [L,NE,R=301]

【讨论】:

  • 谢谢你,它似乎工作。仍然无法使用 wordpress 多站点域映射,但它解决了我的问题。
【解决方案2】:

您能否尝试使用所示示例进行以下、编写和测试。请确保在测试 URL 之前清除浏览器缓存。

RewriteEngine ON
##For adding nl to home/base page.
RewriteRule ^/?$ http://%{HTTP_HOST}/nl [R=301,NE,L]

##For dealing with pages NOT starting with nl or fr here.
RewriteCond %{REQUEST_URI} !^/(nl|fr) [NC]
RewriteRule ^ /nl%{REQUEST_URI} [R=301,NE,L]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-06
    • 2011-03-14
    • 2020-08-11
    • 1970-01-01
    • 1970-01-01
    • 2012-06-27
    相关资源
    最近更新 更多