htaccess 重写子目录中的获取参数并使用 php 检索

htaccess rewrite get parameter in subdirectory and retrieve with php

我尝试将 example.com/subdir/file.php?param=123 重写为 example.com/subdir/file/123 并检索 php 中的值。

在我写的主目录的.htaccess RewriteRule ^/subdir/([^/.]+)/?$ /subdir/file.php?param= [L,NC,QSA] 但它不起作用。

您可以在站点根目录 .htaccess 中尝试此规则:

RewriteRule ^(subdir/[\w-]+)/([\w-]+)/?$ .php?param= [L,NC,QSA]
  • 模式需要匹配 file 部分也在 subdir/
  • 之后
  • 使用捕获组和反向引用来避免重复相同的文本
  • 正则表达式模式中的前导 /.htaccess
  • 中不匹配
  • 这假设 subdir/ 中没有其他 .htaccess。