正则表达式在测试时有效,但不在 .htaccess 中工作


RegEx works when tested but not in .htaccess

我有这个正则表达式,当我在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 in subdomain

<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]

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

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]

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