mod_rewrite and mod_alias

#1
Hi,

I'm using litespeed over Directadmin and use "Apache Configuration File" for vhosts configurations,

I just define this alise rules in apache config file:

Alias /TestURI "/var/www/html/test.html"

mentioned rules function properly when I use it directly by a domain,
Code:
http://www.domain.com/TestURI
(it shows the content of /var/www/html/test.html which is ok)

but alias rules don't work when I call them from a "mod_rewrite" rule, something like this:

RewriteEngine On
RewriteRule .* /TestURI [L]

(mentioned rule is returned by a "404 not found" error)

seems that "mod_alias" are loaded after "mod_rewrite" ones,
Since mentioned rules are working fine in Apache web service, it there any chance to have them work in litespeed as well?

Thanks,
 
Last edited by a moderator:

NiteWave

Administrator
#2
my tests shows that apache will return 500 error for that rewriterule. while lsws will return 404 Not Found

here's detail:
Code:
/var/www/html>cat .htaccess
#Options Indexes
RewriteEngine On
RewriteRule .* /TestURI [L]
#RewriteRule .* /TestURI [PT]

/var/www/html>ls -al
drwxr-xr-x 11 root root 4096 May 14 03:34 .
drwxr-xr-x  7 root root 4096 Jun 15  2015 ..
-rw-r--r--  1 root root   92 May 14 03:32 .htaccess
-rw-r--r--  1 root root    0 Sep  8  2014 index.html
-rw-r--r--  1 root root    0 May 14 03:19 test.html

/var/www/html>curl -I 127.0.0.1/test2.html
HTTP/1.1 500 Internal Server Error
Date: Sat, 14 May 2016 07:53:03 GMT
Server: Apache/2.2.15 (CentOS)
Connection: close
Content-Type: text/html; charset=iso-8859-1

/var/www/html>curl -I 127.0.0.1:2080/test2.html
HTTP/1.1 404 Not Found
Date: Sat, 14 May 2016 07:53:14 GMT
Accept-Ranges: bytes
Server: LiteSpeed
Connection: Keep-Alive
 
#3
thanks for your time and attention,

let me explain the issue in more detail,
Assume that, we have this shared account here (based on direct-admin shared hosting structure):

/home/accountname/domains/domain.com/public_html/

(our account name is accountname and the domain is domain.com )

this alias rule also has been defined in main apache config httpd.conf:

Alias /TestURI "/var/www/html/test.html"

(actually it makes all
Code:
http://domain.com/TestURI
to show the content of /var/www/html/test.html )
the content of "/home/accountname/domains/domain.com/public_html/.htaccess" is as follow:

RewriteEngine On
RewriteRule PublicLink /TestURI [L]


(this should make this address
Code:
http://domain.com/PublicLink
shows the content of
Code:
http://domain.com/TestURI
which will be /var/www/html/test.html )

here is the results:
-------------------------------
# curl -I domain.com/TestURI (this is fine)
HTTP/1.1 200 OK
Content-Type: text/html
Date: Sat, 14 May 2016 09:24:15 GMT
Accept-Ranges: bytes
Server: LiteSpeed
Connection: close

# curl -I domain.com/PublicLink (this should show the contents of /var/www/html/test.html )
HTTP/1.1 404 Not Found
Cache-Control: public, max-age=60
Expires: Sat, 14 May 2016 09:25:01 GMT
Content-Type: text/html; charset=utf-8
Date: Sat, 14 May 2016 09:24:01 GMT
Accept-Ranges: bytes
Server: LiteSpeed
Connection: close

thanks,
Abed.
 
Last edited by a moderator:

NiteWave

Administrator
#4
yes, my test result is same as yours regarding litespeed.

my point is: what's the test result under apache ?
while litespeed return "404 Not Found"
apache return "500 Internal Server Error"

because of the rewriterule
RewriteRule PublicLink /TestURI [L]

it looks apache not meet your expectation either. and "404 Not Found" is a bit better than "500 Internal Server Error" in user's point of view.
 
#5
Thanks again for your time,

the apache result is "200 OK" for both URLs:

domain.com/TestURI ("200 OK" in apache)
domain.com/PublicLink ("200 OK" in apache)

but litespeed return "404 Not Found" for the url that matches mod_rewrite rules:

domain.com/TestURI ("200 OK" in LiteSpeed)
domain.com/PublicLink ("404 Not Found" in LiteSpeed)

as far as I checked the litespeed logs, seems that the "context" dosent change to "/var/www/html/" (or alias doesnt work) when it's loaded from a mod_rewrite rule,

thanks,
 

NiteWave

Administrator
#6
there is no difference for domain.com/TestURI between apache and litespeed. both are "200 OK"

I don't use DirectAdmin, but apache with simplest config file.
as you see above my test, the apache version is 2.2.15. your apache's version ? 2.4.x ?

you can enable apache's rewrite log, then access domain.com/PublicLink, to know the detail rewrite info.
 
#7
Sure,

here is the logs:

apache 2.4 logs:

# curl domain.com/PublicLink -I
HTTP/1.1 200 OK
Date: Sat, 14 May 2016 23:08:06 GMT
Server: Apache
Last-Modified: Sat, 14 May 2016 22:50:48 GMT
ETag: "c-532d538f8d4d7"
Accept-Ranges: bytes
Content-Length: 12
Vary: User-Agent
Content-Type: text/html

mod rewirte log:

Code:
[Sun May 15 03:22:01.818444 2016] [rewrite:trace2] [pid 18162] mod_rewrite.c(476): [client x.x.x.x:45540] x.x.x.x - - [domain.com/sid#43be038][rid#70d2a00/initial] init rewrite engine with requested uri /PublicLink
[Sun May 15 03:22:01.818481 2016] [rewrite:trace1] [pid 18162] mod_rewrite.c(476): [client x.x.x.x:45540] x.x.x.x - - [domain.com/sid#43be038][rid#70d2a00/initial] pass through /PublicLink
[Sun May 15 03:22:01.819927 2016] [rewrite:trace3] [pid 18162] mod_rewrite.c(476): [client x.x.x.x:45540] x.x.x.x - - [domain.com/sid#43be038][rid#70d2a00/initial] [perdir /home/accountname/domains/domain.com/public_html/] strip per-dir prefix: /home/accountname/domains/domain.com/public_html/PublicLink -> PublicLink
[Sun May 15 03:22:01.819952 2016] [rewrite:trace3] [pid 18162] mod_rewrite.c(476): [client x.x.x.x:45540] x.x.x.x - - [domain.com/sid#43be038][rid#70d2a00/initial] [perdir /home/accountname/domains/domain.com/public_html/] applying pattern 'PublicLink' to uri 'PublicLink'
[Sun May 15 03:22:01.819971 2016] [rewrite:trace2] [pid 18162] mod_rewrite.c(476): [client x.x.x.x:45540] x.x.x.x - - [domain.com/sid#43be038][rid#70d2a00/initial] [perdir /home/accountname/domains/domain.com/public_html/] rewrite 'PublicLink' -> '/TestURI'
[Sun May 15 03:22:01.819989 2016] [rewrite:trace2] [pid 18162] mod_rewrite.c(476): [client x.x.x.x:45540] x.x.x.x - - [domain.com/sid#43be038][rid#70d2a00/initial] [perdir /home/accountname/domains/domain.com/public_html/] trying to replace context docroot /home/accountname/domains/domain.com/public_html with context prefix
[Sun May 15 03:22:01.820005 2016] [rewrite:trace1] [pid 18162] mod_rewrite.c(476): [client x.x.x.x:45540] x.x.x.x - - [domain.com/sid#43be038][rid#70d2a00/initial] [perdir /home/accountname/domains/domain.com/public_html/] internal redirect with /TestURI [INTERNAL REDIRECT]
[Sun May 15 03:22:01.820114 2016] [rewrite:trace2] [pid 18162] mod_rewrite.c(476): [client x.x.x.x:45540] x.x.x.x - - [domain.com/sid#43be038][rid#710ed60/initial/redir#1] init rewrite engine with requested uri /TestURI
[Sun May 15 03:22:01.820144 2016] [rewrite:trace1] [pid 18162] mod_rewrite.c(476): [client x.x.x.x:45540] x.x.x.x - - [domain.com/sid#43be038][rid#710ed60/initial/redir#1] pass through /TestURI
LiteSpeed logs:

# curl domain.com/PublicLink -I
HTTP/1.1 404 Not Found
Cache-Control: public, max-age=60
Expires: Sat, 14 May 2016 23:11:13 GMT
Content-Type: text/html; charset=utf-8
Date: Sat, 14 May 2016 23:10:13 GMT
Accept-Ranges: bytes
Server: LiteSpeed
Connection: close

litespeed error log (domain.com/PublicLink):
Code:
2016-05-15 03:31:39.373 [DEBUG] [y.y.y.y:80] New connection from x.x.x.x:51337, concurrent: 0.
2016-05-15 03:31:39.373 [DEBUG] [x.x.x.x:51337] concurrent conn: 1
2016-05-15 03:31:39.373 [DEBUG] [x.x.x.x:51337] NtwkIOLink::handleEvents() events=1!
2016-05-15 03:31:39.373 [DEBUG] [x.x.x.x:51337] HttpSession::onReadEx(), state: 1!
2016-05-15 03:31:39.373 [DEBUG] [x.x.x.x:51337] readToHeaderBuf().
2016-05-15 03:31:39.373 [DEBUG] [x.x.x.x:51337] Read from client: 407
2016-05-15 03:31:39.373 [DEBUG] [x.x.x.x:51337] read 407 bytes to header buffer
2016-05-15 03:31:39.373 [DEBUG] [x.x.x.x:51337] client accept GZIP encoding.
2016-05-15 03:31:39.373 [DEBUG] [x.x.x.x:51337] processHeader() return 0, header state: 3.
2016-05-15 03:31:39.373 [DEBUG] [x.x.x.x:51337] readToHeaderBuf() return 0.
2016-05-15 03:31:39.373 [DEBUG] [x.x.x.x:51337] processNewReq(), request header buffer size: 411, header used: 411, processed: 411.
2016-05-15 03:31:39.373 [DEBUG] [x.x.x.x:51337] Headers: GET /PublicLink HTTP/1.1
2016-05-15 03:31:39.373 [NOTICE] [x.x.x.x:51337#APVH_www.domain.com] Content len: 0, Request line: 'GET /PublicLink HTTP/1.1'
2016-05-15 03:31:39.373 [DEBUG] [x.x.x.x:51337#APVH_www.domain.com] NtwkIOLink::suspendRead()...
2016-05-15 03:31:39.373 [DEBUG] [x.x.x.x:51337#APVH_www.domain.com] Find context with URI: [/], location: [/home/accountname/domains/domain.com/public_html/]
2016-05-15 03:31:39.373 [DEBUG] [x.x.x.x:51337#APVH_www.domain.com] Find .htaccess context with URI: [/], location: [/home/accountname/domains/domain.com/public_html/]
2016-05-15 03:31:39.373 [INFO] [x.x.x.x:51337#APVH_www.domain.com] File not found [/home/accountname/domains/domain.com/public_html/TestURI]
2016-05-15 03:31:39.373 [DEBUG] [x.x.x.x:51337#APVH_www.domain.com] processContextPath() return 26
2016-05-15 03:31:39.373 [DEBUG] [x.x.x.x:51337#APVH_www.domain.com] processNewReq() return 26.
2016-05-15 03:31:39.373 [DEBUG] [x.x.x.x:51337#APVH_www.domain.com] HttpSession::sendHttpError(),code=404 Not Found
2016-05-15 03:31:39.373 [DEBUG] [x.x.x.x:51337#APVH_www.domain.com] redirect to: URI=[/404.shtml], QueryString=[]
2016-05-15 03:31:39.373 [DEBUG] [x.x.x.x:51337#APVH_www.domain.com] Find .htaccess context with URI: [/], location: [/home/accountname/domains/domain.com/public_html/]
2016-05-15 03:31:39.373 [DEBUG] [x.x.x.x:51337#APVH_www.domain.com] Error Page, processContextPath() return 0
2016-05-15 03:31:39.373 [DEBUG] [x.x.x.x:51337#APVH_www.domain.com] processContextPath() return 0
2016-05-15 03:31:39.373 [DEBUG] [x.x.x.x:51337#APVH_www.domain.com] content type: [text/html], pMIME: 0x124fe40
2016-05-15 03:31:39.373 [DEBUG] [x.x.x.x:51337#APVH_www.domain.com] gzip flag: 3
2016-05-15 03:31:39.374 [DEBUG] [x.x.x.x:51337#APVH_www.domain.com] GZIP response body!
2016-05-15 03:31:39.374 [DEBUG] [x.x.x.x:51337#APVH_www.domain.com] setupGzipBuf() begin GZIP stream.
2016-05-15 03:31:39.374 [DEBUG] [x.x.x.x:51337#APVH_www.domain.com] SSI Process component: 0, offset: 0 , len: 144
2016-05-15 03:31:39.374 [DEBUG] [x.x.x.x:51337#APVH_www.domain.com] Append pre-compressed: 121, Dyn resp body size: 131.
2016-05-15 03:31:39.374 [DEBUG] [x.x.x.x:51337#APVH_www.domain.com] SSI Process component: 2, offset: 0 , len: 0
2016-05-15 03:31:39.374 [DEBUG] [x.x.x.x:51337#APVH_www.domain.com] Dyn resp body size: 131.
2016-05-15 03:31:39.374 [DEBUG] [x.x.x.x:51337#APVH_www.domain.com] SSI Process component: 0, offset: 175 , len: 39
2016-05-15 03:31:39.374 [DEBUG] [x.x.x.x:51337#APVH_www.domain.com] SSI Process component: 2, offset: 0 , len: 0
2016-05-15 03:31:39.374 [DEBUG] [x.x.x.x:51337#APVH_www.domain.com] Dyn resp body size: 131.
2016-05-15 03:31:39.374 [DEBUG] [x.x.x.x:51337#APVH_www.domain.com] SSI Process component: 0, offset: 243 , len: 272
2016-05-15 03:31:39.374 [DEBUG] [x.x.x.x:51337#APVH_www.domain.com] Append pre-compressed: 28, Dyn resp body size: 226.
2016-05-15 03:31:39.374 [DEBUG] [x.x.x.x:51337#APVH_www.domain.com] endResponse( 1 )
2016-05-15 03:31:39.374 [DEBUG] [x.x.x.x:51337#APVH_www.domain.com] endResponseInternal()
2016-05-15 03:31:39.374 [DEBUG] [x.x.x.x:51337#APVH_www.domain.com] endResponse() end GZIP stream.
2016-05-15 03:31:39.374 [DEBUG] [x.x.x.x:51337#APVH_www.domain.com] HttpSession::flush()!
2016-05-15 03:31:39.374 [DEBUG] [x.x.x.x:51337#APVH_www.domain.com] NtwkIOLink written: 547
2016-05-15 03:31:39.374 [DEBUG] [x.x.x.x:51337#APVH_www.domain.com] writeRespBody() sent: 236
2016-05-15 03:31:39.374 [DEBUG] [x.x.x.x:51337#APVH_www.domain.com] writeRespBody() return 236, Dyn response body sent: 236
2016-05-15 03:31:39.374 [DEBUG] [x.x.x.x:51337#APVH_www.domain.com] flushBody() return 0
2016-05-15 03:31:39.374 [DEBUG] [x.x.x.x:51337#APVH_www.domain.com] mark COMPLETE.
2016-05-15 03:31:39.374 [DEBUG] [x.x.x.x:51337#APVH_www.domain.com] NtwkIOLink::continueWrite()...
2016-05-15 03:31:39.374 [DEBUG] [x.x.x.x:51337#APVH_www.domain.com] write resumed!
2016-05-15 03:31:39.374 [DEBUG] [x.x.x.x:51337#APVH_www.domain.com] NtwkIOLink::handleEvents() events=4!
2016-05-15 03:31:39.374 [DEBUG] [x.x.x.x:51337#APVH_www.domain.com] HttpSession::nextRequest()!
2016-05-15 03:31:39.374 [DEBUG] [x.x.x.x:51337#APVH_www.domain.com] Non-KeepAlive, CLOSING!
2016-05-15 03:31:39.374 [DEBUG] [x.x.x.x:51337#APVH_www.domain.com] NtwkIOLink::suspendWrite()...
2016-05-15 03:31:39.374 [DEBUG] [x.x.x.x:51337#APVH_www.domain.com] Shutting down out-bound socket ...
2016-05-15 03:31:39.388 [DEBUG] [x.x.x.x:51337#APVH_www.domain.com] NtwkIOLink::handleEvents() events=16!
2016-05-15 03:31:39.388 [DEBUG] [x.x.x.x:51337#APVH_www.domain.com] Close socket ...
2016-05-15 03:31:39.388 [DEBUG] [x.x.x.x:51337#APVH_www.domain.com] Available Connections: 4850, concurrent conn: 0
2016-05-15 03:31:39.388 [DEBUG] [x.x.x.x:51337#APVH_www.domain.com] Recycle NtwkIoLink
 

NiteWave

Administrator
#8
installed apache 2.4.6 and confirmed your result. also did some other testings.

my test result so far:
1. in httpd.conf, put in
Alias /TestURI "/var/www/html/test.html"

2. in .htaccess, put in
RewriteEngine On
RewriteRule PublicLink /TestURI [L]

then when access domain.com/PublicLink
a)apache 2.2 -- 500 Internal Error
b)apache 2.4 -- 200 OK
c)lsws --- 404 Not Found

3. in httpd.conf, within <VirtualHost> block, put in
RewriteEngine On
RewriteRule /PublicLink /TestURI [L]

then when access domain.com/PublicLink
a)apache 2.2 -- 500 Internal Error
b)apache 2.4 -- 404 Not Found
c)lsws --- 200 OK

4. in httpd.conf, within <VirtualHost> block, put in
RewriteEngine On
RewriteRule /PublicLink /TestURI [L,PT]

then when access domain.com/PublicLink
a)apache 2.2 -- 500 Internal Error
b)apache 2.4 -- 200 OK
c)lsws --- 200 OK
 
Last edited:
#9
thank you for your consideration,
that's true, seems that mentioned mod_rewrite rules work only when they are entered directly in "VirtualHost" tag in litespeed,
as far as I checked, the rules not working even in "directory" or "location" tags as well,
is there any chance to have them work in ".htaccess" or in tag "directory"?
 
#10
I didn't test those reweiterule under <DIrectory>, search apache document, it's valid -- I haven't experienced this case before ( rewrite rule inside <Directory> block), I believe this is vary rare case in practice.

anyway I did tests, my result:
5. in httpd.conf, within <Directory /var/www/html> block, put in
RewriteEngine On
RewriteRule dir.* /TestURI [L]
or
RewriteEngine On
RewriteRule dir.* /TestURI [L,PT]

then when access domain.com/dirxxxx
a)apache 2.2 -- 500 Internal Error
b)apache 2.4 -- 404 Not Found
c)lsws --- 404 Not Found

can you verify above result on apache 2.4 ? if so, even apache 2.4 have implemented this feature yet.

based on the facts we have discussed above, I don't think update lsws code here is feasible although this will be decided by developer.

the practical advice for you:
1, use above option 4. so it's working on both lsws and apache 2.4
or
2. use other solution which is easy to maintain, and compatible with apache 2.2 / 2.4 / lsws.
 
#11
thanks,

Please notice that if you use these rules in a <directory> tag:

RewriteEngine On
RewriteRule dir.* /TestURI [L]

then you have to disable (or remove) mod_rewrite rules in ".htaccess" otherwise all the configuration will be overwritten by the ones in .htaccess.

as far as I checked mentioned rules (in tag <directory>) works in apache 2.4 (I dont have apache 2.2 to test on this version), they dont work in lsws,

thanks again,
 
#12
I did tests again under apache 2.4, my results shows
if <Directory> inside <VirtualHost>. then return "200 OK"
if out of <VirtualHost> (server level <Directory>), then return "404 Not Found"

<VirtualHost *:80>
DocumentRoot /var/www/html

<Directory "/var/www/html/sub">
RewriteEngine On
RewriteRule dir.* /TestURI [L]
</Directory>

</VirtualHost>
then domain.com/sub/dirxxxx
return "200 OK" in apache 2.4

I have changed the test URL in .htaccess as below:
/var/www/html>cat .htaccess
RewriteEngine On
RewriteRule ht.* /TestURI [L]

so test URL under <Directory> , use domain.com/sub/dirxxx
test URL in .htaccess, use domain.com/htxyz
 
Top