CupOfTea696
CupOfTea696

Reputation: 1295

RegEx works when tested but not in .htaccess

I have this regular expression that works perfectly when I test it in RegExr (http://regexr.com?36ms0), however when I add it to my .htaccess it never matches.

RegEx:

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

This regex should match strings like "testphp" and "test.rar" but not "test.php" or any of the other included extensions.

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

Purpose of this .htaccess file is to redirect any request to the index, apart from any file ending in .php, .html, .css, jpg, ... and any file or directory that exists. Browsing to http://mysite.com/test/ would redirect to the index. Browsing to http://mysite.com/test.php would show the file or a 404 if it doesn't exist. Browsing to http://mysite.com/test.rar would download the file or redirect to the index if it doesn't exist.

I have another .htaccess in my server root:

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]

Upvotes: 1

Views: 202

Answers (2)

anubhava
anubhava

Reputation: 785146

You can simplify the regex and your rule:

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]

Upvotes: 1

Andrew Hoos
Andrew Hoos

Reputation: 1520

Im betting you problem is that whatever is reading your regex doesn't understand perl type regular expressions like (?! and (?:, but I have no idea what is reading that file.

Upvotes: 0

Related Questions