[Resolved] htaccess double not condition failing

Status
Not open for further replies.
#1
I'm a client on Interserver's LiteSpeed/5.0.17 Enterprise system

Note: foobar.org is Not my actual website name.

The following rules are supposed to redirect everything not HTTPS or not starting with www. except robots.txt and anything with directory /ExternalConnect to https://www.foobar.org/%{REQUEST_URI}

It works as expected when I test at http://htaccess.madewithlove.be/ and mostly works on my site, but when tested on my site with www.foobar.org/ExternalConnect/ext.php (does not exist) it redirects with a 301 then throws a 404 for file 404.shmtl Expected result is 404 for /ExternalConnect/ext.php with no redirection.

When tested live on my site with https://foobar.org/ExternalConnect/Ext.php (exists) it redirects with a 301 then executes the script Expected result is a 200 with no redirection

When tested live on my site with https://www.foobar.org/ExternalConnect/ext.php (does not exist) it redirects throws a 404 /ExternalConnect/ext.php as expected

I've also tried RewriteCond %{REQUEST_URI} !(^/robots.txt$|/ExternalConnect) without success for the above. Also the robots.txt logic works as expected. So essentially this rule is not working RewriteCond: %{REQUEST_URI} !/ExternalConnect

.htaccess Ruleset
Code:
RewriteCond %{REQUEST_URI} !^/robots.txt$
RewriteCond %{REQUEST_URI} !/ExternalConnect
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^foobar\.org [NC]
RewriteRule ^(.*)$ https://www.foobar.org/$1 [R=301,L]

PS I also tried the following but also does not work as expected
RewriteCond %{REQUEST_URI} !^/robots.txt$
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^foobar\.org [NC]
RewriteRule "!ExternalConnect" "https://www.foobar.org/%{REQUEST_URI}" [R=301,L]
 
Last edited by a moderator:

NiteWave

Administrator
#2
My test results are different with yours.
I cloned your .htaccess
Code:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/robots.txt$
RewriteCond %{REQUEST_URI} !/ExternalConnect
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^foobar\.org [NC]
RewriteRule ^(.*)$ https://www.foobar.org/$1 [R=301,L]

/usr/local/lsws/DEFAULT/html>ls -l ExternalConnect/
total 0
-rw-r--r-- 1 root root 0 May 12 23:26 Ext.php
/usr/local/lsws/DEFAULT/html>curl -I -H "host:foobar.org" 127.0.0.1:8088/ExternalConnect/Ext.php
HTTP/1.1 200 OK
X-Powered-By: PHP/5.6.20
Content-Type: text/html; charset=UTF-8
Date: Fri, 13 May 2016 03:38:13 GMT
Accept-Ranges: bytes
Server: LiteSpeed
Connection: Keep-Alive

/usr/local/lsws/DEFAULT/html>mv ExternalConnect/ delete-ExternalConnect
/usr/local/lsws/DEFAULT/html>ls -l ExternalConnect/
ls: cannot access ExternalConnect/: No such file or directory

/usr/local/lsws/DEFAULT/html>curl -I -H "host:foobar.org" 127.0.0.1:8088/ExternalConnect/Ext.php
HTTP/1.1 404 Not Found
ETag: "c3-540d0c03-5b8d58c2"
Last-Modified: Mon, 08 Sep 2014 01:53:07 GMT
Content-Type: text/html
Content-Length: 195
Date: Fri, 13 May 2016 03:39:11 GMT
Accept-Ranges: bytes
Server: LiteSpeed
Connection: Keep-Alive
the result is what as expected ?
 

NiteWave

Administrator
#3
Sorry, just found my previous test have problem -- I didn't enable "AllowOverride All" in native virtual host, thus the result.

I did tests again, and confirmed the issue you reported. then enabled rewrite log, found the cause and hence the workaround.
Code:
/usr/local/lsws/DEFAULT/html>ls -l ExternalConnect/
-rw-r--r-- 1 root root 0 May 12 23:26 Ext.php

/usr/local/lsws/DEFAULT/html>curl -I -H "host:foobar.org" 127.0.0.1:8088/ExternalConnect/Ext.php
HTTP/1.1 200 OK
X-Powered-By: PHP/5.6.20
Content-Type: text/html; charset=UTF-8
Date: Tue, 17 May 2016 04:49:04 GMT
Accept-Ranges: bytes
Server: LiteSpeed
Connection: Keep-Alive

==> above is what expected result ?

/usr/local/lsws/DEFAULT/html>curl -I -H "host:foobar.org" 127.0.0.1:8088/ExternalConnect/Ext2.php
HTTP/1.1 301 Moved Permanently
Date: Tue, 17 May 2016 04:40:39 GMT
Accept-Ranges: bytes
Server: LiteSpeed
Location: https://www.foobar.org/error404.html
Connection: Keep-Alive

==> above reproduce the issue ?
rewrite log:
2016-05-17 00:55:31.693 [INFO] [127.0.0.1:34286] [REWRITE] strip base: '/' from URI: '/ExternalConnect/Ext2.php'
2016-05-17 00:55:31.693 [INFO] [127.0.0.1:34286] [REWRITE] Rule: Match 'ExternalConnect/Ext2.php' with pattern '^(.*)$', result: 2
2016-05-17 00:55:31.693 [INFO] [127.0.0.1:34286] [REWRITE] Cond: Match '/ExternalConnect/Ext2.php' with pattern '^/robots.txt$', result: -1
2016-05-17 00:55:31.693 [INFO] [127.0.0.1:34286] [REWRITE] Cond: Match '/ExternalConnect/Ext2.php' with pattern '/ExternalConnect', result: 1
2016-05-17 00:55:31.693 [INFO] [127.0.0.1:34286] File not found [/usr/local/lsws/DEFAULT/html/ExternalConnect/Ext2.php]
2016-05-17 00:55:31.693 [INFO] [127.0.0.1:34286] [REWRITE] strip base: '/' from URI: '/error404.html'
2016-05-17 00:55:31.693 [INFO] [127.0.0.1:34286] [REWRITE] Rule: Match 'error404.html' with pattern '^(.*)$', result: 2
2016-05-17 00:55:31.693 [INFO] [127.0.0.1:34286] [REWRITE] Cond: Match '/error404.html' with pattern '^/robots.txt$', result: -1
2016-05-17 00:55:31.693 [INFO] [127.0.0.1:34286] [REWRITE] Cond: Match '/error404.html' with pattern '/ExternalConnect', result: -1
2016-05-17 00:55:31.693 [INFO] [127.0.0.1:34286] [REWRITE] Cond: Match 'off' with pattern 'off', result: 1
2016-05-17 00:55:31.693 [INFO] [127.0.0.1:34286] [REWRITE] Source URI: 'error404.html' => Result URI: 'https://www.foobar.org/error404.html'
2016-05-17 00:55:31.693 [INFO] [127.0.0.1:34286] [REWRITE] Last Rule, stop!
explain:
Code:
1) access 127.0.0.1:8088/ExternalConnect/Ext2.php
2) it matchs
RewriteCond %{REQUEST_URI} !/ExternalConnect
3) immediately, lsws found /usr/local/lsws/DEFAULT/html/ExternalConnect/Ext2.php not exist and ready to return /error404.html
4) /error404.html also go through the whole rewrite rules, and result this 301 redirect !
the solution(or call it workaround) is:
Code:
add
RewriteCond %{REQUEST_URI} !^/error404.html$
before
RewriteCond %{REQUEST_URI} !^/robots.txt$

in your case, use 404.shtml instead of error404.html
 
Status
Not open for further replies.
Top