.htaccess重写规则;t影响常规文件请求


.htaccess RewriteRule that doesn't affect regular file requests?

我正试图为运行在Apache上的web应用程序设置一个短URL服务。我被它的.htaccess部分难住了,其中的请求像"http://shrt.url/username"answers"http://longer.url/username"应该总是调用我的forward.php文件。但我也需要规则忽略常规请求(如对图像和脚本的请求)。

当只有1个/&没有.的样子?谢谢

What does a RewriteRule which calls up forward.php ONLY when there's just 1 / & no .'s look like?

不太清楚你在寻找什么,但也许这一款适合你:

RewriteEngine On
# if not a file    
RewriteCond %{REQUEST_FILENAME} !-f
# if not a directory
RewriteCond %{REQUEST_FILENAME} !-d
# URI has one / and no dots then forward to /forward.php internally
RewriteRule ^([^/.]+)/?$ /forward.php?q=$1 [L]

参考:Apache mod_rewrite简介