htaccess中的rewriterule以匹配某些文件扩展名

时间:2010-10-27 22:02:35

标签: regex .htaccess mod-rewrite

如何查找某些文件扩展名的实例,例如。(jpg | png | css | js | php),如果没有匹配,请将其发送到index.php?route = $ 1.

我希望能够允许自定义用户名的句点。

所以,将http://example.com/my.name重写为index.php?route = my.name

当前设置:

htaccess的:

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^.]+)$ index.php?route=$1 [QSA,L]
</IfModule>

什么有效:
http://example.com/userpage - &gt;的index.php?路径= userpage
http://example.com/userpage/photos - &gt;的index.php?路径= userpage /照片
http://example.com/file.js - &gt; http://example.com/file.js
http://example.com/css/file.css - &gt; http://example.com/css/file.css

除了上述内容我还需要工作:
http://example.com/my.name - &gt; index.php?route = my.name

2 个答案:

答案 0 :(得分:22)

添加额外的RewriteCond以排除您不想重写的条件。在正则表达式之前使用!表示匹配的任何文件都应该使条件失败。下面的RewriteCond是未经测试的,但应该让你知道你需要什么:

RewriteCond %{REQUEST_URI} !\.(jpg|png|css|js|php)$

答案 1 :(得分:5)

您是否尝试过逆转逻辑?像

这样的东西
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule \.(jpg|png|css|js)$ - [L]

对于任何带有.jpg,.png,.css或.js扩展名的文件,都不会重写。然后添加现有规则,以便将非文件,非目录请求重新路由到index.php。