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

时间:2022-08-10 10:51:28

How can I look for an instance for certain file extensions, like .(jpg|png|css|js|php), and if there is NOT a match send it to index.php?route=$1.

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

I would like to be able to allow period's for custom usernames.

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

So, rewrite http://example.com/my.name to index.php?route=my.name

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

Current setup:

目前的设置:

.htaccess:

的.htaccess:

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

What works:
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/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

What I need to work in addition to above:
http://example.com/my.name -> index.php?route=my.name

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

2 个解决方案

#1


21  

Add an extra RewriteCond to exclude the conditions that you don't want rewritten. Use a ! before the regular expression to indicate that any files matching should fail the condition. The RewriteCond below is untested, but should give you an idea of what you need:

添加额外的RewriteCond以排除您不想重写的条件。用一个 !在正则表达式之前,指示任何匹配的文件都应该使条件失败。下面的RewriteCond未经测试,但应该让您了解您的需求:

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

#2


5  

Have you tried reversing the logic? Something like

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

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

This will not do a rewrite for any file with a .jpg, .png, .css, or .js extension. Then add your existing rules so that non-file, non-directory requests get rerouted to index.php.

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

#1


21  

Add an extra RewriteCond to exclude the conditions that you don't want rewritten. Use a ! before the regular expression to indicate that any files matching should fail the condition. The RewriteCond below is untested, but should give you an idea of what you need:

添加额外的RewriteCond以排除您不想重写的条件。用一个 !在正则表达式之前,指示任何匹配的文件都应该使条件失败。下面的RewriteCond未经测试,但应该让您了解您的需求:

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

#2


5  

Have you tried reversing the logic? Something like

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

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

This will not do a rewrite for any file with a .jpg, .png, .css, or .js extension. Then add your existing rules so that non-file, non-directory requests get rerouted to index.php.

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