【问题标题】:redirect url exact match with htaccess like case sensitive重定向 url 与 htaccess 完全匹配,如区分大小写
【发布时间】:2020-07-06 06:53:10
【问题描述】:

如果查询字符串或 index.php 之后的任何内容,重定向 url 与 htaccess 完全匹配不重定向

Redirect This Page: https://example.com/demo/index.php
To Page: https://example.com/ (home page)

But Do Not redirect: https://example.com/demo/index.php/*

 Do NOT redirect: https://example.com/demo/index.php/password

 Do NOT redirect https://example.com/demo/index.php?m=page
  not redirect if any other combination 

only redirect https://example.com/demo/index.php to https://example.com/

这个脚本不工作

RewriteEngine On
RewriteBase /
RewriteRule ^demo/index.php /demo/index.php?m=page[L,NC,END]
RewriteRule ^demo/index.php$ https://example.com/ [L,R=301]

【问题讨论】:

  • 如果第二条规则没有按预期工作,您要么在浏览器中查看缓存结果,要么没有为该主机或位置启用重写模块。您是如何启用对分布式配置文件(“.htaccess”)的解释以及如何确保它们现在被考虑的?
  • 重写模块使其他重定向工作正常。我在其他站点上进行了测试,没有缓存,也没有工作
  • 啊,如果你有多个这样的规则(为什么不显示all?),那么可能是规则顺序的问题。请记住,规则是从上到下处理的。如果您在考虑该规则之前停止重写过程,它确实不会被应用......
  • 你的问题真的一点都不清楚。如果您只想将https://example.com/demo/index.php 重定向到https://example.com/,则只需设置一条规则RewriteRule ^demo/index.php$ https://example.com/ [L,R=301]
  • 这条规则只删除 index.php demo/index.php 重定向到 demo/

标签: php regex apache .htaccess


【解决方案1】:

仅将https://example.com/demo/index.php 重定向到https://example.com/

如果您只想重定向那个 exact URL,而没有查询字符串,那么您需要反转现有规则并包含一个额外的条件来检查 QUERY_STRING 是否为空。 RewriteRule 仅匹配 URL 路径,不包括查询字符串。

您在第一条规则中的 flags 参数之前还缺少一个 空格

例如:

# Redirect exact URL to home page
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^demo/index\.php$ / [R=302,L]

# Redirect other URLs with query string OR path-info
RewriteRule ^demo/index\.php /demo/index.php?m=page [NC,L]

你不需要L END

使用 302 进行测试,只有在确认可以正常工作后才更改为 301。避免缓存问题。

【讨论】:

  • 这个脚本只是删除 index.php demo/index.php?asda=das 重定向到 demo/?asda=das
  • 这很好用 redirectMatch 301 ^/client/index.php$ example.com 但不适用于像 client/index.php?dd=dfd 这样的查询字符串
  • 这是我回答中第一条规则的作用。 (如果您不需要重写(第二条规则)-您在问题中没有提到这一点-然后将其删除?)您无法将查询字符串与 mod_alias RedirectMatch 指令匹配,您需要使用mod_rewrite RewriteRule(如上)匹配查询字符串(以确认查询字符串为空)。
【解决方案2】:

您可以在站点根目录 .htaccess 中使用这些规则:

RewriteEngine On

RewriteCond %{THE_REQUEST} \s/+demo/index\.php\s [NC]
RewriteRule ^ / [L,R=301]

RewriteRule ^demo/index\.php$ $0?m=page [QSA,NC,L]

【讨论】:

    【解决方案3】:

    这是由我经常使用的htaccess redirect generator 创建的。

    RewriteCond %{QUERY_STRING} ^$
    RewriteRule ^demo/index\.php$ https://example.com/? [R=301,L]
    

    也许“?”由于空查询字符串匹配,符号是多余的。

    不确定,但你也可以尝试添加

    RewriteCond %{REQUEST_METHOD} !=POST 
    

    万一你无法登录。一起来:

    RewriteCond %{REQUEST_METHOD} !=POST
    RewriteCond %{QUERY_STRING} ^$
    RewriteRule ^demo/index\.php$ https://example.com/? [R=301,L]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-01
      • 1970-01-01
      相关资源
      最近更新 更多