htaccess 中的重写规则以匹配某些文件扩展名
如何查找某些文件扩展名的实例,例如 .(jpg|png|css|js|php),如果没有匹配项,请将其发送到 index.php?route=$1。
我希望能够允许自定义用户名使用句点。
因此,将 http://example.com/my.name 重写为 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>
什么有效:
http://example.com/userpage -> index.php?route=用户页面
http://example.com/userpage/photos -> index.php?route=用户页面/照片
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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
添加额外的 RewriteCond 以排除您不想重写的条件。在正则表达式之前使用
!
来指示任何匹配的文件都不会满足条件。下面的 RewriteCond 未经测试,但应该可以让您了解您需要什么: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:你有没有尝试过扭转逻辑?像这样的东西
不会对任何具有 .jpg、.png、.css 或 .js 扩展名的文件进行重写。然后添加现有规则,以便将非文件、非目录请求重新路由到index.php。
Have you tried reversing the logic? Something like
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.