首页 > 解决方案 > 重定向 url 与 htaccess 完全匹配,如区分大小写

问题描述

如果查询字符串或 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]

标签: phpregexapache.htaccess

解决方案


只重定向https://example.com/demo/index.phphttps://example.com/

如果您只想重定向那个确切的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。避免缓存问题。


推荐阅读