rewriterule in htaccess to match certain file extensions

杀马特。学长 韩版系。学妹 提交于 2020-01-01 04:17:12

问题


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.

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

Current setup:

.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

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


回答1:


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 %{REQUEST_URI} !\.(jpg|png|css|js|php)$



回答2:


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.



来源:https://stackoverflow.com/questions/4038079/rewriterule-in-htaccess-to-match-certain-file-extensions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!