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

wda*_*vis 13 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>
Run Code Online (Sandbox Code Playgroud)

有用的:
http://example.com/userpage - > index.php?route = userpage
http://example.com/userpage/photos - > index.php?route = userpage/photos
http://example.com /file.js - > http://example.com/file.js
http://example.com/css/file.css - > http://example.com/css/file.css

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

Din*_*ngo 22

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

RewriteCond %{REQUEST_URI} !\.(jpg|png|css|js|php)$
Run Code Online (Sandbox Code Playgroud)


Har*_*lby 5

你试过逆转逻辑吗?就像是

RewriteCond %{REQUEST_FILENAME} -f
RewriteRule \.(jpg|png|css|js)$ - [L]
Run Code Online (Sandbox Code Playgroud)

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