1

当我在 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]
4

2 回答 2

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]
于 2013-10-11T10:25:28.710 回答
0

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

于 2013-10-13T16:20:35.600 回答