.htaccess 重写正则表达式:匹配任何内容,但其他规则必须不受影响

.htaccess rewrite regex: match anything but the other rules must be not affected

我有这个 .htaccess 我想当我输入 exemple.com/home 时它会打开文件 home.php 并登录同样的东西它会打开 hani.php

RewriteRule ^home$  home.php [L]
RewriteRule ^login$  login.php [L]
...
RewriteRule ^([^\/]+)$  hani.php?link= [L]

我的问题当我回家或其他任何时候它会打开hani.php

Please I want a good solution because I have many pages

问题是您的最后一次重写是无条件的,并且正在重写之后之前的其他重写已经完成,因此将所有内容发送到hani.php

有 2 种解决方案(使用任何一种):

  1. 使用以下规则从上一条规则中排除所有文件和目录:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ hani.php?link= [L,QSA]
  1. 从最后一条规则中的匹配模式中排除点,这样 home.php 或任何包含点的请求都不会匹配模式:
RewriteRule ^([^/.]+)/?$ hani.php?link= [L,QSA]

请注意,mod_rewrite.

中的正则表达式模式无需转义 /