Php 我如何通过保持文件名不变来重写url,但传递的参数应该是文件夹名?

Php 我如何通过保持文件名不变来重写url,但传递的参数应该是文件夹名?,php,.htaccess,url-rewriting,Php,.htaccess,Url Rewriting,我正在传递一个将用作子文件夹的名称,但文件名保持不变,例如foldername/gallery.php?q=new_folder应该是new_folder/gallery.php,使用htaccess 附加我试图做的事情的HTA访问权限 RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)/(gallery.php)$ $2.php?q=$1 [L] galle

我正在传递一个将用作子文件夹的名称,但文件名保持不变,例如foldername/gallery.php?q=new_folder应该是new_folder/gallery.php,使用htaccess

附加我试图做的事情的HTA访问权限

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/(gallery.php)$ $2.php?q=$1 [L]    

gallery.php
是文件名,
$1
基本上是
新文件夹
。我已经为此挣扎了很久。

我刚刚在.htaccess中添加了下面提到的代码。希望这对你有帮助

RewriteEngine On    
RewriteRule ^foldername/(.*) http://example.com/newfolder/gallery.php?options=$1 [R=301,L]

你很接近。您只需要在规则的目标URI中使用
foldername/

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/(gallery\.php)$ foldername/$2.php?q=$1 [L,QSA,NC]

谢谢你的回复。我试过了,但还是不起作用。但是在文件名之前只添加
foldername/
确实有帮助。