我正在使用apache构建一个简单的API,我想将/really/long/path/api/captions.php?caption_id=blah
映射到/really/long/path/api/captions/blah
。不必在重写规则中指定完整路径,这一点很重要,因为无论将代码部署到何处,我都希望它能正常工作。但是,我找不到/找到.htaccess
重写规则的有效示例,该示例使我能够仅基于扩展名的最后一部分进行匹配。
因此,假设我在名为captions.php
的目录中有api
,我需要在.htaccess
中包含什么api
文件才能完成此转换里面有/really/long/path/
?
(我还希望能够将/really/long/path/api/captions.php
映射到/really/long/path/api/captions/
和/really/long/path/api/captions
。)
我尝试了各种类似于通配符的语法;这是这些无效的尝试之一:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*?)/?captions/(.*?)/?$ /captions.php?caption_id=$1 [L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /captions\.php\?caption_id=([^\&\ ]+)
RewriteRule ^(.*?)/?captions\.php$ /captions/%1? [L,R=301]
谢谢!
答案 0 :(得分:0)
最后到达那里。这就是所需要的:
RewriteEngine On # Turn on the rewriting engine
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^captions/?$ captions.php [NC,L]
RewriteRule ^captions/(.*)/?$ captions.php?caption_id=$1 [NC,L]