重写规则htaccess的,以匹配特定的文件扩展名 [英] rewriterule in htaccess to match certain file extensions

查看:164
本文介绍了重写规则htaccess的,以匹配特定的文件扩展名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何查找某些文件扩展名的实例,等等。(JPG | PNG | CSS | JS | PHP),如果有不匹配发送到index.php路线= $ 1

我希望能够让时间的自定义的用户名。

所以,重写 http://example.com/my.name 的index.php?路径= my.name

当前设置:

的.htaccess:

 < IfModule mod_rewrite.c>
RewriteEngine叙述上
的RewriteBase /
的RewriteCond%{} REQUEST_FILENAME!-f
的RewriteCond%{} REQUEST_FILENAME!-d
重写规则^([^。] +)$的index.php?路径= $ 1 [QSA,L]
< / IfModule>
 

什么工作:
http://example.com/userpage ? - >的index.php路线= userpage
http://example.com/userpage/photos ? - >的index.php路线= userpage /照片< BR> 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路线= my.name

解决方案

添加一个额外的RewriteCond排除不希望改写的条件。使用前的常规EX pression指示的任何文件匹配出现故障的情况。以下的RewriteCond是未经测试,但应该给你什么,你需要一个想法:

 的RewriteCond%{REQUEST_URI} \(JPG | PNG | CSS | JS | PHP)!$。
 

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

解决方案

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)$

这篇关于重写规则htaccess的,以匹配特定的文件扩展名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆