简体   繁体   English

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

[英]htaccess rewrite get parameter in subdirectory and retrieve with php

I try rewriting example.com/subdir/file.php?param=123 to example.com/subdir/file/123 and retrieve the value in php.我尝试将example.com/subdir/file.php?param=123重写为example.com/subdir/file/123并检索 php 中的值。

In the .htaccess in the main directory i wrote RewriteRule ^/subdir/([^/.]+)/?$ /subdir/file.php?param=$1 [L,NC,QSA] but it's not working.在主目录的.htaccess中,我写了RewriteRule ^/subdir/([^/.]+)/?$ /subdir/file.php?param=$1 [L,NC,QSA]但它不起作用。

You may try this rule in site root .htaccess:您可以在站点根目录 .htaccess 中尝试此规则:

RewriteRule ^(subdir/[\w-]+)/([\w-]+)/?$ $1.php?param=$2 [L,NC,QSA]
  • Pattern needs to match file part also after subdir/模式也需要在subdir/之后匹配file部分
  • Use a capture group and back-reference to avoid repeating same text使用捕获组和反向引用来避免重复相同的文本
  • Leading / in regex pattern is not matched in .htaccess .htaccess中的前导/正则表达式模式不匹配
  • This assumes there is no other .htaccess in subdir/ .这假设在subdir/中没有其他 .htaccess 。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM