RegEx在测试时有效,但在.htaccess中无效

时间:2013-10-11 10:21:04

标签: php regex apache .htaccess mod-rewrite

当我在RegExr(http://regexr.com?36ms0)中测试时,我有这个正则表达式,但是当我将它添加到我的.htaccess时,它永远不会匹配。

正则表达式:

^(?!.*\.(?:(?:ht|x)ml|j(?:peg|s|pg)|p(?:hp|ng)|[rc]ss|bmp|ico|gif)$).*$

此正则表达式应匹配“testphp”和“test.rar”等字符串,但不能匹配“test.php”或任何其他包含的扩展名。

子域

.htaccess

<ifModule mod_rewrite.c>
    RewriteEngine On

    RewriteBase /

    # handle YouTube token
    RewriteCond %{QUERY_STRING} ^token=(.*)$
    RewriteRule ^login/ login/%1/? [NE,R=301,L]

    # add trailing slash for the looks
    RewriteRule ^(([a-z0-9\-]+/)*[a-z0-9\-]+)$ $1/ [NC,R=301,L]

    # html5 pushstate (history) support:
    # we do want to give error pages for certain file types though.
    RewriteCond %{REQUEST_FILENAME} ^(?!.*?\.((ht|x)ml|j(peg|s|pg)|p(hp|ng)|[rc]ss|bmp|ico|gif)$).*?$ [NC]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !index
    RewriteRule (.*) / [L]
</ifModule>

此.htaccess文件的目的是将任何请求重定向到索引,除了以.php,.html,.css,jpg,...以及任何存在的文件或目录结尾的任何文件。浏览到http://mysite.com/test/会重定向到索引。浏览到http://mysite.com/test.php会显示该文件,如果不存在则会显示404。浏览到http://mysite.com/test.rar会下载文件或重定向到索引(如果不存在)。

我的服务器根目录中有另一个.htaccess:

SetEnv PHPRC /home/ttrcusto/public_html/cgi-bin/php.ini

<Files ~ "\.(ini)$">
order allow,deny
deny from all
</Files>

IndexIgnore .htaccess */.??* *~ *# */HEADER* */README* */_vti*

Redirect /unsubscribe http://ttrcustoms.us/#account=edit
Redirect /cydia http://repo.ttrcustoms.us
Redirect /Cydia http://repo.ttrcustoms.us
Redirect /repo http://repo.ttrcustoms.us
Redirect /tools http://tools.ttrcustoms.us

Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteRule ^repo/deb/(.*)\.deb$ /download.php?package=$1.deb [L,QSA]

2 个答案:

答案 0 :(得分:1)

您可以简化正则表达式和规则:

RewriteEngine On

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule !\.(jpe?g|gif|bmp|png|ico|css|rss|js|html?|xml|php)$ / [L,NC]

答案 1 :(得分:0)

我打赌你的问题是,无论你正在阅读你的正则表达式都不理解perl类型的正则表达式,如(?!(?:,但我不知道是什么读取该文件。