Apache rewrite parses URL twice unexpectedly - apache

I'm trying to redirect all the webpages from certain domain names to a specific web application that handles several sites.
I have my web app in a folder "skylight2" and another folder, named "testsite", with only a .htaccess file:
RewriteEngine on
RewriteRule ^(((?!skylight2).)*)$ /home/stefano/webroot_aliases/skylight2/$1/?site=testsite [QSA]
I'm using virtual roots, and my httpd-vroot.conf looks like this:
<VirtualHost *:80>
ServerName webserver
ServerAlias *.webserver
UseCanonicalName Off
VirtualDocumentRoot /home/stefano/webroot_aliases/%1
Options -Indexes FollowSymLinks -MultiViews
RewriteLog "/var/log/httpd/vroot_rewrite.log"
RewriteLogLevel 3
<Directory /home/stefano/webroot_aliases/>
Order allow,deny
Allow from all
AllowOverride FileInfo Indexes Limit Options
RewriteEngine On
RewriteBase /
</Directory>
</VirtualHost>
If I try to access testsite.webserver, I get an "object not found" error. This is my rewrite log:
127.0.0.1 - - [08/Mar/2012:07:56:23 --0600] [testsite.webserver/sid#21f2ca0][rid#253f320/initial] (3) [perdir /home/stefano/webroot_aliases/testsite/] add path info postfix: /home/stefano/webroot_aliases/testsite/articles -> /home/stefano/webroot_aliases/testsite/articles/view
127.0.0.1 - - [08/Mar/2012:07:56:23 --0600] [testsite.webserver/sid#21f2ca0][rid#253f320/initial] (3) [perdir /home/stefano/webroot_aliases/testsite/] strip per-dir prefix: /home/stefano/webroot_aliases/testsite/articles/view -> articles/view
127.0.0.1 - - [08/Mar/2012:07:56:23 --0600] [testsite.webserver/sid#21f2ca0][rid#253f320/initial] (3) [perdir /home/stefano/webroot_aliases/testsite/] applying pattern '^(((?!skylight2).)*)$' to uri 'articles/view'
127.0.0.1 - - [08/Mar/2012:07:56:23 --0600] [testsite.webserver/sid#21f2ca0][rid#253f320/initial] (2) [perdir /home/stefano/webroot_aliases/testsite/] rewrite 'articles/view' -> '/home/stefano/webroot_aliases/skylight2/articles/view/?site=testsite'
127.0.0.1 - - [08/Mar/2012:07:56:23 --0600] [testsite.webserver/sid#21f2ca0][rid#253f320/initial] (3) split uri=/home/stefano/webroot_aliases/skylight2/articles/view/?site=testsite -> uri=/home/stefano/webroot_aliases/skylight2/articles/view/, args=site=testsite
127.0.0.1 - - [08/Mar/2012:07:56:23 --0600] [testsite.webserver/sid#21f2ca0][rid#253f320/initial] (1) [perdir /home/stefano/webroot_aliases/testsite/] internal redirect with /home/stefano/webroot_aliases/skylight2/articles/view/ [INTERNAL REDIRECT]
127.0.0.1 - - [08/Mar/2012:07:56:23 --0600] [testsite.webserver/sid#21f2ca0][rid#2531a18/initial/redir#1] (3) [perdir /home/stefano/webroot_aliases/testsite/] add path info postfix: /home/stefano/webroot_aliases/testsite/home -> /home/stefano/webroot_aliases/testsite/home/stefano/webroot_aliases/skylight2/articles/view/
127.0.0.1 - - [08/Mar/2012:07:56:23 --0600] [testsite.webserver/sid#21f2ca0][rid#2531a18/initial/redir#1] (3) [perdir /home/stefano/webroot_aliases/testsite/] strip per-dir prefix: /home/stefano/webroot_aliases/testsite/home/stefano/webroot_aliases/skylight2/articles/view/ -> home/stefano/webroot_aliases/skylight2/articles/view/
127.0.0.1 - - [08/Mar/2012:07:56:23 --0600] [testsite.webserver/sid#21f2ca0][rid#2531a18/initial/redir#1] (3) [perdir /home/stefano/webroot_aliases/testsite/] applying pattern '^(((?!skylight2).)*)$' to uri 'home/stefano/webroot_aliases/skylight2/articles/view/'
127.0.0.1 - - [08/Mar/2012:07:56:23 --0600] [testsite.webserver/sid#21f2ca0][rid#2531a18/initial/redir#1] (1) [perdir /home/stefano/webroot_aliases/testsite/] pass through /home/stefano/webroot_aliases/testsite/home
127.0.0.1 - - [08/Mar/2012:07:56:23 --0600] [testsite.webserver/sid#21f2ca0][rid#252f2a0/initial] (3) [perdir /home/stefano/webroot_aliases/testsite/] strip per-dir prefix: /home/stefano/webroot_aliases/testsite/favicon.ico -> favicon.ico
127.0.0.1 - - [08/Mar/2012:07:56:23 --0600] [testsite.webserver/sid#21f2ca0][rid#252f2a0/initial] (3) [perdir /home/stefano/webroot_aliases/testsite/] applying pattern '^(((?!skylight2).)*)$' to uri 'favicon.ico'
127.0.0.1 - - [08/Mar/2012:07:56:23 --0600] [testsite.webserver/sid#21f2ca0][rid#252f2a0/initial] (2) [perdir /home/stefano/webroot_aliases/testsite/] rewrite 'favicon.ico' -> '/home/stefano/webroot_aliases/skylight2/favicon.ico/?site=testsite'
127.0.0.1 - - [08/Mar/2012:07:56:23 --0600] [testsite.webserver/sid#21f2ca0][rid#252f2a0/initial] (3) split uri=/home/stefano/webroot_aliases/skylight2/favicon.ico/?site=testsite -> uri=/home/stefano/webroot_aliases/skylight2/favicon.ico/, args=site=testsite
127.0.0.1 - - [08/Mar/2012:07:56:23 --0600] [testsite.webserver/sid#21f2ca0][rid#252f2a0/initial] (1) [perdir /home/stefano/webroot_aliases/testsite/] internal redirect with /home/stefano/webroot_aliases/skylight2/favicon.ico/ [INTERNAL REDIRECT]
127.0.0.1 - - [08/Mar/2012:07:56:23 --0600] [testsite.webserver/sid#21f2ca0][rid#2533830/initial/redir#1] (3) [perdir /home/stefano/webroot_aliases/testsite/] add path info postfix: /home/stefano/webroot_aliases/testsite/home -> /home/stefano/webroot_aliases/testsite/home/stefano/webroot_aliases/skylight2/favicon.ico/
127.0.0.1 - - [08/Mar/2012:07:56:23 --0600] [testsite.webserver/sid#21f2ca0][rid#2533830/initial/redir#1] (3) [perdir /home/stefano/webroot_aliases/testsite/] strip per-dir prefix: /home/stefano/webroot_aliases/testsite/home/stefano/webroot_aliases/skylight2/favicon.ico/ -> home/stefano/webroot_aliases/skylight2/favicon.ico/
127.0.0.1 - - [08/Mar/2012:07:56:23 --0600] [testsite.webserver/sid#21f2ca0][rid#2533830/initial/redir#1] (3) [perdir /home/stefano/webroot_aliases/testsite/] applying pattern '^(((?!skylight2).)*)$' to uri 'home/stefano/webroot_aliases/skylight2/favicon.ico/'
127.0.0.1 - - [08/Mar/2012:07:56:23 --0600] [testsite.webserver/sid#21f2ca0][rid#2533830/initial/redir#1] (1) [perdir /home/stefano/webroot_aliases/testsite/] pass through /home/stefano/webroot_aliases/testsite/home
Apparently, the URL is parsed once (correctly), and then the second time where it's added the prefix again, resulting in an error. Unfortunately, I cannot put a [L] flag to this rule because the URL gets parsed further in the application folder.
Can anybody explain to me what is happening?
Thanks
gm
EDIT: I applied the rule:
RewriteRule ^(?!skylight2)(.*)$ skylight2/$1/?site=testsite [QSA,L]
And get this:
127.0.0.1 - - [09/Mar/2012:07:22:45 --0600] [testsite.webserver/sid#161fca0][rid#1951280/initial] (3) [perdir /home/stefano/webroot_aliases/testsite/] add path info postfix: /home/stefano/webroot_a
liases/testsite/articles -> /home/stefano/webroot_aliases/testsite/articles/view
127.0.0.1 - - [09/Mar/2012:07:22:45 --0600] [testsite.webserver/sid#161fca0][rid#1951280/initial] (3) [perdir /home/stefano/webroot_aliases/testsite/] strip per-dir prefix: /home/stefano/webroot_al
iases/testsite/articles/view -> articles/view
127.0.0.1 - - [09/Mar/2012:07:22:45 --0600] [testsite.webserver/sid#161fca0][rid#1951280/initial] (3) [perdir /home/stefano/webroot_aliases/testsite/] applying pattern '^(?!skylight2)(.*)$' to uri
'articles/view'
127.0.0.1 - - [09/Mar/2012:07:22:45 --0600] [testsite.webserver/sid#161fca0][rid#1951280/initial] (2) [perdir /home/stefano/webroot_aliases/testsite/] rewrite 'articles/view' -> 'skylight2/articles
/view/?site=testsite'
127.0.0.1 - - [09/Mar/2012:07:22:45 --0600] [testsite.webserver/sid#161fca0][rid#1951280/initial] (3) split uri=skylight2/articles/view/?site=testsite -> uri=skylight2/articles/view/, args=site=tes
tsite
127.0.0.1 - - [09/Mar/2012:07:22:45 --0600] [testsite.webserver/sid#161fca0][rid#1951280/initial] (3) [perdir /home/stefano/webroot_aliases/testsite/] add per-dir prefix: skylight2/articles/view/ -
> /home/stefano/webroot_aliases/testsite/skylight2/articles/view/
127.0.0.1 - - [09/Mar/2012:07:22:45 --0600] [testsite.webserver/sid#161fca0][rid#1951280/initial] (1) [perdir /home/stefano/webroot_aliases/testsite/] internal redirect with /home/stefano/webroot_a
liases/testsite/skylight2/articles/view/ [INTERNAL REDIRECT]
127.0.0.1 - - [09/Mar/2012:07:22:45 --0600] [testsite.webserver/sid#161fca0][rid#195c9e0/initial/redir#1] (3) [perdir /home/stefano/webroot_aliases/testsite/] add path info postfix: /home/stefano/w
ebroot_aliases/testsite/home -> /home/stefano/webroot_aliases/testsite/home/stefano/webroot_aliases/testsite/skylight2/articles/view/
127.0.0.1 - - [09/Mar/2012:07:22:45 --0600] [testsite.webserver/sid#161fca0][rid#195c9e0/initial/redir#1] (3) [perdir /home/stefano/webroot_aliases/testsite/] strip per-dir prefix: /home/stefano/we
broot_aliases/testsite/home/stefano/webroot_aliases/testsite/skylight2/articles/view/ -> home/stefano/webroot_aliases/testsite/skylight2/articles/view/
127.0.0.1 - - [09/Mar/2012:07:22:45 --0600] [testsite.webserver/sid#161fca0][rid#195c9e0/initial/redir#1] (3) [perdir /home/stefano/webroot_aliases/testsite/] applying pattern '^(?!skylight2)(.*)$'
to uri 'home/stefano/webroot_aliases/testsite/skylight2/articles/view/'
127.0.0.1 - - [09/Mar/2012:07:22:45 --0600] [testsite.webserver/sid#161fca0][rid#195c9e0/initial/redir#1] (2) [perdir /home/stefano/webroot_aliases/testsite/] rewrite 'home/stefano/webroot_aliases/
testsite/skylight2/articles/view/' -> 'skylight2/home/stefano/webroot_aliases/testsite/skylight2/articles/view//?site=testsite'
127.0.0.1 - - [09/Mar/2012:07:22:45 --0600] [testsite.webserver/sid#161fca0][rid#195c9e0/initial/redir#1] (3) split uri=skylight2/home/stefano/webroot_aliases/testsite/skylight2/articles/view//?sit
e=testsite -> uri=skylight2/home/stefano/webroot_aliases/testsite/skylight2/articles/view//, args=site=testsite&site=testsite
127.0.0.1 - - [09/Mar/2012:07:22:45 --0600] [testsite.webserver/sid#161fca0][rid#195c9e0/initial/redir#1] (3) [perdir /home/stefano/webroot_aliases/testsite/] add per-dir prefix: skylight2/home/ste
fano/webroot_aliases/testsite/skylight2/articles/view// -> /home/stefano/webroot_aliases/testsite/skylight2/home/stefano/webroot_aliases/testsite/skylight2/articles/view//
127.0.0.1 - - [09/Mar/2012:07:22:45 --0600] [testsite.webserver/sid#161fca0][rid#195c9e0/initial/redir#1] (1) [perdir /home/stefano/webroot_aliases/testsite/] internal redirect with /home/stefano/w
ebroot_aliases/testsite/skylight2/home/stefano/webroot_aliases/testsite/skylight2/articles/view// [INTERNAL REDIRECT]
And so on in an infinite loop.
Why does apache keep adding the path info? Neither L nor DPI flags work.
EDIT #2
Following TerryE's advice I used this config:
RewriteEngine On
RewriteBase /testsite
RewriteRule ^(?!skylight2)(.*)$ /skylight2/$1?site=testsite [QSA,L]
And got this:
127.0.0.1 - - [10/Mar/2012:08:16:58 --0600] [testsite.webserver/sid#1575ca0][rid#2d6a740/initial] (3) [perdir /home/stefano/webroot_aliases/testsite/] add path info postfix: /home/stefano/webroot_aliases/testsite/articles -> /home/stefano/webroot_aliases/testsite/articles/view
127.0.0.1 - - [10/Mar/2012:08:16:58 --0600] [testsite.webserver/sid#1575ca0][rid#2d6a740/initial] (3) [perdir /home/stefano/webroot_aliases/testsite/] strip per-dir prefix: /home/stefano/webroot_aliases/testsite/articles/view -> articles/view
127.0.0.1 - - [10/Mar/2012:08:16:58 --0600] [testsite.webserver/sid#1575ca0][rid#2d6a740/initial] (3) [perdir /home/stefano/webroot_aliases/testsite/] applying pattern '^(?!skylight2)(.*)$' to uri 'articles/view'
127.0.0.1 - - [10/Mar/2012:08:16:58 --0600] [testsite.webserver/sid#1575ca0][rid#2d6a740/initial] (2) [perdir /home/stefano/webroot_aliases/testsite/] rewrite 'articles/view' -> '/skylight2/articles/view?site=testsite'
127.0.0.1 - - [10/Mar/2012:08:16:58 --0600] [testsite.webserver/sid#1575ca0][rid#2d6a740/initial] (3) split uri=/skylight2/articles/view?site=testsite -> uri=/skylight2/articles/view, args=site=testsite
127.0.0.1 - - [10/Mar/2012:08:16:58 --0600] [testsite.webserver/sid#1575ca0][rid#2d6a740/initial] (2) [perdir /home/stefano/webroot_aliases/testsite/] trying to replace prefix /home/stefano/webroot_aliases/testsite/ with /testsite
127.0.0.1 - - [10/Mar/2012:08:16:58 --0600] [testsite.webserver/sid#1575ca0][rid#2d6a740/initial] (1) [perdir /home/stefano/webroot_aliases/testsite/] internal redirect with /skylight2/articles/view [INTERNAL REDIRECT]
127.0.0.1 - - [10/Mar/2012:08:16:58 --0600] [testsite.webserver/sid#1575ca0][rid#18a9968/initial/redir#1] (3) [perdir /home/stefano/webroot_aliases/testsite/] add path info postfix: /home/stefano/webroot_aliases/testsite/skylight2 -> /home/stefano/webroot_aliases/testsite/skylight2/articles/view
127.0.0.1 - - [10/Mar/2012:08:16:58 --0600] [testsite.webserver/sid#1575ca0][rid#18a9968/initial/redir#1] (3) [perdir /home/stefano/webroot_aliases/testsite/] strip per-dir prefix: /home/stefano/webroot_aliases/testsite/skylight2/articles/view -> skylight2/articles/view
127.0.0.1 - - [10/Mar/2012:08:16:58 --0600] [testsite.webserver/sid#1575ca0][rid#18a9968/initial/redir#1] (3) [perdir /home/stefano/webroot_aliases/testsite/] applying pattern '^(?!skylight2)(.*)$' to uri 'skylight2/articles/view'
127.0.0.1 - - [10/Mar/2012:08:16:58 --0600] [testsite.webserver/sid#1575ca0][rid#18a9968/initial/redir#1] (1) [perdir /home/stefano/webroot_aliases/testsite/] pass through /home/stefano/webroot_aliases/testsite/skylight2
127.0.0.1 - - [10/Mar/2012:08:16:58 --0600] [testsite.webserver/sid#1575ca0][rid#18b1290/initial] (3) [perdir /home/stefano/webroot_aliases/testsite/] strip per-dir prefix: /home/stefano/webroot_aliases/testsite/favicon.ico -> favicon.ico
127.0.0.1 - - [10/Mar/2012:08:16:58 --0600] [testsite.webserver/sid#1575ca0][rid#18b1290/initial] (3) [perdir /home/stefano/webroot_aliases/testsite/] applying pattern '^(?!skylight2)(.*)$' to uri 'favicon.ico'
127.0.0.1 - - [10/Mar/2012:08:16:58 --0600] [testsite.webserver/sid#1575ca0][rid#18b1290/initial] (2) [perdir /home/stefano/webroot_aliases/testsite/] rewrite 'favicon.ico' -> '/skylight2/favicon.ico?site=testsite'
127.0.0.1 - - [10/Mar/2012:08:16:58 --0600] [testsite.webserver/sid#1575ca0][rid#18b1290/initial] (3) split uri=/skylight2/favicon.ico?site=testsite -> uri=/skylight2/favicon.ico, args=site=testsite
127.0.0.1 - - [10/Mar/2012:08:16:58 --0600] [testsite.webserver/sid#1575ca0][rid#18b1290/initial] (2) [perdir /home/stefano/webroot_aliases/testsite/] trying to replace prefix /home/stefano/webroot_aliases/testsite/ with /testsite
127.0.0.1 - - [10/Mar/2012:08:16:58 --0600] [testsite.webserver/sid#1575ca0][rid#18b1290/initial] (1) [perdir /home/stefano/webroot_aliases/testsite/] internal redirect with /skylight2/favicon.ico [INTERNAL REDIRECT]
127.0.0.1 - - [10/Mar/2012:08:16:58 --0600] [testsite.webserver/sid#1575ca0][rid#18a96f0/initial/redir#1] (3) [perdir /home/stefano/webroot_aliases/testsite/] add path info postfix: /home/stefano/webroot_aliases/testsite/skylight2 -> /home/stefano/webroot_aliases/testsite/skylight2/favicon.ico
127.0.0.1 - - [10/Mar/2012:08:16:58 --0600] [testsite.webserver/sid#1575ca0][rid#18a96f0/initial/redir#1] (3) [perdir /home/stefano/webroot_aliases/testsite/] strip per-dir prefix: /home/stefano/webroot_aliases/testsite/skylight2/favicon.ico -> skylight2/favicon.ico
127.0.0.1 - - [10/Mar/2012:08:16:58 --0600] [testsite.webserver/sid#1575ca0][rid#18a96f0/initial/redir#1] (3) [perdir /home/stefano/webroot_aliases/testsite/] applying pattern '^(?!skylight2)(.*)$' to uri 'skylight2/favicon.ico'
127.0.0.1 - - [10/Mar/2012:08:16:58 --0600] [testsite.webserver/sid#1575ca0][rid#18a96f0/initial/redir#1] (1) [perdir /home/stefano/webroot_aliases/testsite/] pass through /home/stefano/webroot_aliases/testsite/skylight2
Apache throws a "File does not exist: /home/stefano/webroot_aliases/testsite/skylight2" error. The correct URL should be instead /home/stefano/webroot_aliases/skylight2/articles/view.
Could it be because the VirtualDocumentRoot is set to testsite and I can't get out of it once it's set?
By the way, I'm using Apache 2.2.22.
EDIT #3
I actually solved it by placing a symlink "skylight2" in testsite, pointing to "../skylight2". thanks for the help!!

I've come across this bug before and there was an old bugrep on it which fizzled out. It can occur if you do a fall-through on a Per Directory context. But since the rewrite engine loops over the htaccess files anyway, the [L] doesn't mean last it really means "restart loop".
Next your rewrite rule is wrong since your document root is home/stefano/webroot_aliases and rule targets are in URI space not file space so your target should just be skylight2/$1/?site=testsite.
Next the Negative lookahead assertion should not be inside the (.*) loop, so try:
RewriteRule ^(?!skylight2)(.*)$ skylight2/$1/?site=testsite [QSA,L]
If this doesn't work, then append the new rewrite.log extract. Thanks.
Update following comments
The rewrite log shows a DOCROOT of /home/stefano/webroot_aliases/testsite and redirects
articles/view -> testsite/skylight2/articles/view/?site=testsite
Which fails the negative lookahead and loops again, so this is a case when you do need the leading / on the replacement string:
RewriteBase /testsite
RewriteRule ^(?!skylight2)(.*)$ /skylight2/$1/?site=testsite [QSA,L]

Related

mod_rewrite END flag not working in httpd 2.4

The END flag for mod_rewrite in Apache 2.4 does not appear to be working correctly. I have a directory named test with a file test.html. I have placed an .htaccess file in the directory with the following content
RewriteEngine on
RewriteRule ^test$ test.html [NC,QSA,END]
RewriteRule ^$ test.html [NC,QSA,END]
Here is the rewrite log for the request http://localhost.dev/test/
[...] 10.0.2.2 - - [localhost.dev/sid#2532c00][rid#2a56d60/initial] [perdir /var/www/vhosts/test/] strip per-dir prefix: /var/www/vhosts/test/ ->
[...] 10.0.2.2 - - [localhost.dev/sid#2532c00][rid#2a56d60/initial] [perdir /var/www/vhosts/test/] applying pattern '^test$' to uri ''
[...] 10.0.2.2 - - [localhost.dev/sid#2532c00][rid#2a56d60/initial] [perdir /var/www/vhosts/test/] strip per-dir prefix: /var/www/vhosts/test/ ->
[...] 10.0.2.2 - - [localhost.dev/sid#2532c00][rid#2a56d60/initial] [perdir /var/www/vhosts/test/] applying pattern '^$' to uri ''
[...] 10.0.2.2 - - [localhost.dev/sid#2532c00][rid#2a56d60/initial] [perdir /var/www/vhosts/test/] rewrite '' -> 'test.html'
[...] 10.0.2.2 - - [localhost.dev/sid#2532c00][rid#2a56d60/initial] [perdir /var/www/vhosts/test/] add per-dir prefix: test.html -> /var/www/vhosts/test/test.html
[...] 10.0.2.2 - - [localhost.dev/sid#2532c00][rid#2a56d60/initial] [perdir /var/www/vhosts/test/] strip document_root prefix: /var/www/vhosts/test/test.html -> /test/test.html
[...] 10.0.2.2 - - [localhost.dev/sid#2532c00][rid#2a56d60/initial] [perdir /var/www/vhosts/test/] internal redirect with /test/test.html [INTERNAL REDIRECT]
[...] 10.0.2.2 - - [localhost.dev/sid#2532c00][rid#2a44060/subreq] [perdir /var/www/vhosts/test/] strip per-dir prefix: /var/www/vhosts/test/index.php -> index.php
[...] 10.0.2.2 - - [localhost.dev/sid#2532c00][rid#2a44060/subreq] [perdir /var/www/vhosts/test/] applying pattern '^test$' to uri 'index.php'
[...] 10.0.2.2 - - [localhost.dev/sid#2532c00][rid#2a44060/subreq] [perdir /var/www/vhosts/test/] strip per-dir prefix: /var/www/vhosts/test/index.php -> index.php
[...] 10.0.2.2 - - [localhost.dev/sid#2532c00][rid#2a44060/subreq] [perdir /var/www/vhosts/test/] applying pattern '^$' to uri 'index.php'
[...] 10.0.2.2 - - [localhost.dev/sid#2532c00][rid#2a44060/subreq] [perdir /var/www/vhosts/test/] pass through /var/www/vhosts/test/index.php
[...] 10.0.2.2 - - [localhost.dev/sid#2532c00][rid#2a44060/subreq] [perdir /var/www/vhosts/test/] strip per-dir prefix: /var/www/vhosts/test/index.html -> index.html
[...] 10.0.2.2 - - [localhost.dev/sid#2532c00][rid#2a44060/subreq] [perdir /var/www/vhosts/test/] applying pattern '^test$' to uri 'index.html'
[...] 10.0.2.2 - - [localhost.dev/sid#2532c00][rid#2a44060/subreq] [perdir /var/www/vhosts/test/] strip per-dir prefix: /var/www/vhosts/test/index.html -> index.html
[...] 10.0.2.2 - - [localhost.dev/sid#2532c00][rid#2a44060/subreq] [perdir /var/www/vhosts/test/] applying pattern '^$' to uri 'index.html'
[...] 10.0.2.2 - - [localhost.dev/sid#2532c00][rid#2a44060/subreq] [perdir /var/www/vhosts/test/] pass through /var/www/vhosts/test/index.html
Why is mod_rewrite making sub requests for directory index files index.html and index.php, when the second rule matches the request?
Request for http://localhost.dev/test/test works without any sub request, as expected
[...] 10.0.2.2 - - [localhost.dev/sid#2532c00][rid#2a58b20/initial] [perdir /var/www/vhosts/test/] strip per-dir prefix: /var/www/vhosts/test/test -> test
[...] 10.0.2.2 - - [localhost.dev/sid#2532c00][rid#2a58b20/initial] [perdir /var/www/vhosts/test/] applying pattern '^test$' to uri 'test'
[...] 10.0.2.2 - - [localhost.dev/sid#2532c00][rid#2a58b20/initial] [perdir /var/www/vhosts/test/] rewrite 'test' -> 'test.html'
[...] 10.0.2.2 - - [localhost.dev/sid#2532c00][rid#2a58b20/initial] [perdir /var/www/vhosts/test/] add per-dir prefix: test.html -> /var/www/vhosts/test/test.html
[...] 10.0.2.2 - - [localhost.dev/sid#2532c00][rid#2a58b20/initial] [perdir /var/www/vhosts/test/] strip document_root prefix: /var/www/vhosts/test/test.html -> /test/test.html
[...] 10.0.2.2 - - [localhost.dev/sid#2532c00][rid#2a58b20/initial] [perdir /var/www/vhosts/test/] internal redirect with /test/test.html [INTERNAL REDIRECT]
I have tried using L and END flags, independently and together.
It appears mod_dir does not respect the END flag set by mod_rewrite. For anyone else who is experiencing this, I was able to work around the problem with the following
RewriteEngine on
RewriteRule ^$ /test/home.php [NC,QSA,END,E=IS_DIR_INDEX:1]
FallbackResource /test/fallback.php
<If "env('IS_DIR_INDEX') == 1">
SetHandler default_handler
</If>
I hit the same problem on Apache 2.4.7 on Ubuntu and reported a bug to Apache which was confirmed and is fixed in 2.4.9, see here. For Ubuntu, there is an updated PPA here for easy upgrade. If upgrading is not possible or you don't want to compile the sources, you can use Joyce's workaround above, or the one I posted here which uses only RewriteCond and RewriteRule.

Conditionally redirecting traffic with mod_rewrite dependent on protocol

I've been writing a rewrite rule to try and redirect all of our traffic from mysite.com.au to www.mysite.com.au. I also need to conditionally redirect depending on SSL enabled or not.
I've hacked on a bit from askapache.com to create a variable that holds either http or https dependently. Below is the code I'm using:
RewriteCond %{HTTPS} =on
RewriteRule ^(.+)$ - [env=ps:https]
RewriteCond %{HTTPS} !=on
RewriteRule ^(.+)$ - [env=ps:http]
RewriteCond %{HTTP_HOST} ^mysite\.com\.au(.*)$ [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^(.*)$ %{ENV:ps}://www.mysite.com.au/$1 [L,R=301]
This works fine for URLs with paths (e.g. mysite.com.au/foo redirects to www.mysite.com.au/foo) and does the http and https switch fine.
However when just accessing the domain (just mysite.com.au) it redirects to http://www.mysite.com.au/home/website/public_html/:/www.mysite.com.au/
I'm sure it's something very small and basic I'm missing - if someone could point it out, that'd be appreciated.
Thanks!
Andrew
I've found an answer - it's nothing like what it seems from first glance.
I turned on RewriteLog and I identified the problem. In the logs if I am using my original condition/rule and use mysite.com.au/test I see the following:
10.1.1.1 - - [11/Feb/2013:11:37:50 +1100] [mysite.com.au/sid#b81d24f8][rid#b847d100/initial] (3) [perdir /home/website/public_html/] strip per-dir prefix: /home/website/public_html/test -> test
10.1.1.1 - - [11/Feb/2013:11:37:50 +1100] [mysite.com.au/sid#b81d24f8][rid#b847d100/initial] (3) [perdir /home/website/public_html/] applying pattern '^(.+)$' to uri 'test'
10.1.1.1 - - [11/Feb/2013:11:37:50 +1100] [mysite.com.au/sid#b81d24f8][rid#b847d100/initial] (3) [perdir /home/website/public_html/] strip per-dir prefix: /home/website/public_html/test -> test
10.1.1.1 - - [11/Feb/2013:11:37:50 +1100] [mysite.com.au/sid#b81d24f8][rid#b847d100/initial] (3) [perdir /home/website/public_html/] applying pattern '^(.+)$' to uri 'test'
10.1.1.1 - - [11/Feb/2013:11:37:50 +1100] [mysite.com.au/sid#b81d24f8][rid#b847d100/initial] (3) [perdir /home/website/public_html/] strip per-dir prefix: /home/website/public_html/test -> test
10.1.1.1 - - [11/Feb/2013:11:37:50 +1100] [mysite.com.au/sid#b81d24f8][rid#b847d100/initial] (3) [perdir /home/website/public_html/] applying pattern '^(.*)$' to uri 'test'
10.1.1.1 - - [11/Feb/2013:11:37:50 +1100] [mysite.com.au/sid#b81d24f8][rid#b847d100/initial] (2) [perdir /home/website/public_html/] rewrite 'test' -> 'http://www.mysite.com.au/test'
10.1.1.1 - - [11/Feb/2013:11:37:50 +1100] [mysite.com.au/sid#b81d24f8][rid#b847d100/initial] (2) [perdir /home/website/public_html/] explicitly forcing redirect with http://www.mysite.com.au/test
However when I run it against just mysite.com.au I see:
10.1.1.1 - - [11/Feb/2013:11:38:16 +1100] [mysite.com.au/sid#b81d24f8][rid#b845bc18/initial] (3) [perdir /home/website/public_html/] strip per-dir prefix: /home/website/public_html/ ->
10.1.1.1 - - [11/Feb/2013:11:38:16 +1100] [mysite.com.au/sid#b81d24f8][rid#b845bc18/initial] (3) [perdir /home/website/public_html/] applying pattern '^(.+)$' to uri ''
10.1.1.1 - - [11/Feb/2013:11:38:16 +1100] [mysite.com.au/sid#b81d24f8][rid#b845bc18/initial] (3) [perdir /home/website/public_html/] strip per-dir prefix: /home/website/public_html/ ->
10.1.1.1 - - [11/Feb/2013:11:38:16 +1100] [mysite.com.au/sid#b81d24f8][rid#b845bc18/initial] (3) [perdir /home/website/public_html/] applying pattern '^(.+)$' to uri ''
10.1.1.1 - - [11/Feb/2013:11:38:16 +1100] [mysite.com.au/sid#b81d24f8][rid#b845bc18/initial] (3) [perdir /home/website/public_html/] strip per-dir prefix: /home/website/public_html/ ->
10.1.1.1 - - [11/Feb/2013:11:38:16 +1100] [mysite.com.au/sid#b81d24f8][rid#b845bc18/initial] (3) [perdir /home/website/public_html/] applying pattern '^(.*)$' to uri ''
10.1.1.1 - - [11/Feb/2013:11:38:16 +1100] [mysite.com.au/sid#b81d24f8][rid#b845bc18/initial] (2) [perdir /home/website/public_html/] rewrite '' -> '://www.mysite.com.au/'
10.1.1.1 - - [11/Feb/2013:11:38:16 +1100] [mysite.com.au/sid#b81d24f8][rid#b845bc18/initial] (3) [perdir /home/website/public_html/] add per-dir prefix: ://www.mysite.com.au/ -> /home/website/public_html/://www.mysite.com.au/
10.1.1.1 - - [11/Feb/2013:11:38:16 +1100] [mysite.com.au/sid#b81d24f8][rid#b845bc18/initial] (2) [perdir /home/website/public_html/] explicitly forcing redirect with http:/.makeitmine.com.au/home/website/public_html/://www.mysite.com.au/
Note that the environment variable I’m trying to set (SSL on/off) isn’t being set. As such, it’s not seeing the http/https prefix and assuming it’s a relative internal path!
When I’m trying to set the environment variable, I use the following code:
RewriteCond %{HTTPS} =on
RewriteRule ^(.+)$ - [env=ps:https]
RewriteCond %{HTTPS} !=on
RewriteRule ^(.+)$ - [env=ps:http]
The RegEx of ^(.+)$ expects a minimum of 1 character, which there is not to match! I modified it as follows:
RewriteCond %{HTTPS} =on
RewriteRule ^(.*)$ - [env=ps:https]
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ - [env=ps:http]
And now it matches, even on 0 characters!
Thanks again for your help, problem solved :)
10.1.1.1 - - [11/Feb/2013:11:44:57 +1100] [mysite.com.au/sid#b81d24f8][rid#b8478208/initial] (3) [perdir /home/website/public_html/] strip per-dir prefix: /home/website/public_html/ ->
10.1.1.1 - - [11/Feb/2013:11:44:57 +1100] [mysite.com.au/sid#b81d24f8][rid#b8478208/initial] (3) [perdir /home/website/public_html/] applying pattern '^(.*)$' to uri ''
10.1.1.1 - - [11/Feb/2013:11:44:57 +1100] [mysite.com.au/sid#b81d24f8][rid#b8478208/initial] (3) [perdir /home/website/public_html/] strip per-dir prefix: /home/website/public_html/ ->
10.1.1.1 - - [11/Feb/2013:11:44:57 +1100] [mysite.com.au/sid#b81d24f8][rid#b8478208/initial] (3) [perdir /home/website/public_html/] applying pattern '^(.*)$' to uri ''
10.1.1.1 - - [11/Feb/2013:11:44:57 +1100] [mysite.com.au/sid#b81d24f8][rid#b8478208/initial] (3) [perdir /home/website/public_html/] strip per-dir prefix: /home/website/public_html/ ->
10.1.1.1 - - [11/Feb/2013:11:44:57 +1100] [mysite.com.au/sid#b81d24f8][rid#b8478208/initial] (3) [perdir /home/website/public_html/] applying pattern '^(.*)$' to uri ''
10.1.1.1 - - [11/Feb/2013:11:44:57 +1100] [mysite.com.au/sid#b81d24f8][rid#b8478208/initial] (2) [perdir /home/website/public_html/] rewrite '' -> 'http://www.mysite.com.au/'
10.1.1.1 - - [11/Feb/2013:11:44:57 +1100] [mysite.com.au/sid#b81d24f8][rid#b8478208/initial] (2) [perdir /home/website/public_html/] explicitly forcing redirect with http://www.mysite.com.au/

Apache Mod Rewrite does not rewrite file name

i have the following problem:
On the download dialog on each browser it displays me only the dot extension filename
like .divx
But i want the full name eg
http://example.com/14558/.divx rewrites to 14558.divx and not onyly .divx
I want see on every browser dialog the number and the extension.
like 14558.divx
How i can solve this problem ??
RewriteRule ^([0-9]+)/.divx$ $1.divx
RewriteRule ^([0-9]+)/.mkv$ $1.mkv
RewriteRule ^([0-9]+)/.avi$ $1.avi
Current Configuration:
RewriteRule ^([0-9]+)/\.(divx|mkv|avi)$ /$1.$2
Rewrite Log:
127.0.0.1 - - [19/Dec/2011:13:07:02 +0100] [video.local/sid#4518f8][rid#ae5c7c8/initial] (3) [perdir F:/media/video_files/] add path info postfix: F:/media/video_files/29 -> F:/media/video_files/29/.mkv
127.0.0.1 - - [19/Dec/2011:13:07:02 +0100] [video.local/sid#4518f8][rid#ae5c7c8/initial] (3) [perdir F:/media/video_files/] strip per-dir prefix: F:/media/video_files/29/.mkv -> 29/.mkv
127.0.0.1 - - [19/Dec/2011:13:07:02 +0100] [video.local/sid#4518f8][rid#ae5c7c8/initial] (3) [perdir F:/media/video_files/] applying pattern '^([0-9]+)/\.(divx|mkv|avi)$' to uri '29/.mkv'
127.0.0.1 - - [19/Dec/2011:13:07:02 +0100] [video.local/sid#4518f8][rid#ae5c7c8/initial] (2) [perdir F:/media/video_files/] rewrite '29/.mkv' -> '/29.mkv'
127.0.0.1 - - [19/Dec/2011:13:07:02 +0100] [video.local/sid#4518f8][rid#ae5c7c8/initial] (1) [perdir F:/media/video_files/] internal redirect with /29.mkv [INTERNAL REDIRECT]
127.0.0.1 - - [19/Dec/2011:13:07:02 +0100] [video.local/sid#4518f8][rid#ae588f8/initial/redir#1] (3) [perdir F:/media/video_files/] strip per-dir prefix: F:/media/video_files/29.mkv -> 29.mkv
127.0.0.1 - - [19/Dec/2011:13:07:02 +0100] [video.local/sid#4518f8][rid#ae588f8/initial/redir#1] (3) [perdir F:/media/video_files/] applying pattern '^([0-9]+)/\.(divx|mkv|avi)$' to uri '29.mkv'
127.0.0.1 - - [19/Dec/2011:13:07:02 +0100] [video.local/sid#4518f8][rid#ae588f8/initial/redir#1] (1) [perdir F:/media/video_files/] pass through F:/media/video_files/29.mkv
Your rewriterules seem ok... err well maybe it's the backslash. By the way you could optimize in a nicer way (not tested):
RewriteRule ^([0-9]+)/\.(divx|mkv|avi)$ /$1.$2
But the only way to solve your problem (= show the whole filename) is to make a full redirect to a URL that has the full filename.
So in your case redirect would mean these rules:
RewriteRule ^([0-9]+)/\.(divx|mkv|avi)$ $1.$2 [QSA,R=301,L]
And that implies that the URLs like "14558.divx" and "1526.mkv" have to be valid and return the file.
Two hints:
Please try to use the RewriteLog directive: it helps you to track down such problems:
# Trace:
# (!) file gets big quickly, remove in prod environments:
RewriteLog "/web/logs/mywebsite.rewrite.log"
RewriteLogLevel 9
RewriteEngine On
My favorite tool to check for regexp:
http://www.quanetic.com/Regex (don't forget to choose ereg(POSIX) instead of preg(PCRE)!)

htaccess re-applies a matched rule constantly and ends up in a loop

According to http://htaccess.madewithlove.be/ the following ruleset works perfectly:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^images/([^\?]+)$ /_lib/mask_images.php [L]
RewriteRule ^search/([^\?]+)/merchant/([^\?]+)/brand/([^\?]+)/page/([^\?]+)$ /search.php?keyword=$1&merchant=$2&brand=$3&page=$4 [L]
RewriteRule ^search/([^\?]+)/brand/([^\?]+)/page/([^\?]+)$ /search.php?keyword=$1&brand=$2&page=$3 [L]
RewriteRule ^search/([^\?]+)/merchant/([^\?]+)/page/([^\?]+)$ /search.php?keyword=$1&merchant=$2&page=$3 [L]
RewriteRule ^search/([^\?]+)/page/([^\?]+)$ /search.php?keyword=$1&page=$2 [L]
RewriteRule ^search/([^\?]+)/brand/([^\?]+)$ /search.php?keyword=$1&brand=$2 [L]
RewriteRule ^search/([^\?]+)/merchant/([^\?]+)$ /search.php?keyword=$1&merchant=$2 [L]
RewriteRule ^search/([^\?]+)/merchant/([^\?]+)/brand/([^\?]+)$ /search.php?keyword=$1&merchant=$2&brand=$3 [L]
RewriteRule ^search/([^\?]+)$ /search.php?keyword=$1 [L]
RewriteRule ([^\?]+)/productID/([^\?]+)$ /detail.php?keyword=$1&productID=$2 [L]
RewriteRule ([^\?]+)$ /detail.php?keyword=$1 [L]
The issue is that when the last 2 rules are applied and matched, Apache re-applies the matched rules (and correctly assigned arguments) and goes into an infinite loop.
I added:
RewriteCond %{REQUEST_URI} !search.php$
RewriteCond %{REQUEST_URI} !detail.php$
RewriteCond %{REQUEST_URI} !index.php$
to the above ruleset. Again, verified and working by the htaccess tester website, it now stops processing and outputs "detail.php" as the argument.
Here is what the Rewrite Log says:
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaabfcc50/initial] (3) [perdir /var/www/html/] strip per-dir prefix: /var/www/html/hello -> hello
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaabfcc50/initial] (3) [perdir /var/www/html/] applying pattern '^images/(.*)$' to uri 'hello'
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaabfcc50/initial] (3) [perdir /var/www/html/] strip per-dir prefix: /var/www/html/hello -> hello
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaabfcc50/initial] (3) [perdir /var/www/html/] applying pattern '^search/(.*)/merchant/(.*)/brand/(.*)/page/(.*)$' to uri 'hello'
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaabfcc50/initial] (3) [perdir /var/www/html/] strip per-dir prefix: /var/www/html/hello -> hello
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaabfcc50/initial] (3) [perdir /var/www/html/] applying pattern '^search/(.*)/brand/(.*)/page/(.*)$' to uri 'hello'
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaabfcc50/initial] (3) [perdir /var/www/html/] strip per-dir prefix: /var/www/html/hello -> hello
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaabfcc50/initial] (3) [perdir /var/www/html/] applying pattern '^search/(.*)/merchant/(.*)/page/(.*)$' to uri 'hello'
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaabfcc50/initial] (3) [perdir /var/www/html/] strip per-dir prefix: /var/www/html/hello -> hello
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaabfcc50/initial] (3) [perdir /var/www/html/] applying pattern '^search/(.*)/page/(.*)$' to uri 'hello'
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaabfcc50/initial] (3) [perdir /var/www/html/] strip per-dir prefix: /var/www/html/hello -> hello
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaabfcc50/initial] (3) [perdir /var/www/html/] applying pattern '^search/(.*)/brand/(.*)$' to uri 'hello'
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaabfcc50/initial] (3) [perdir /var/www/html/] strip per-dir prefix: /var/www/html/hello -> hello
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaabfcc50/initial] (3) [perdir /var/www/html/] applying pattern '^search/(.*)/merchant/(.*)$' to uri 'hello'
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaabfcc50/initial] (3) [perdir /var/www/html/] strip per-dir prefix: /var/www/html/hello -> hello
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaabfcc50/initial] (3) [perdir /var/www/html/] applying pattern '^search/(.*)/merchant/(.*)/brand/(.*)$' to uri 'hello'
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaabfcc50/initial] (3) [perdir /var/www/html/] strip per-dir prefix: /var/www/html/hello -> hello
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaabfcc50/initial] (3) [perdir /var/www/html/] applying pattern '^search/(.*)$' to uri 'hello'
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaabfcc50/initial] (3) [perdir /var/www/html/] strip per-dir prefix: /var/www/html/hello -> hello
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaabfcc50/initial] (3) [perdir /var/www/html/] applying pattern '(.*)/productID/(.*)$' to uri 'hello'
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaabfcc50/initial] (3) [perdir /var/www/html/] strip per-dir prefix: /var/www/html/hello -> hello
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaabfcc50/initial] (3) [perdir /var/www/html/] applying pattern '(.*)$' to uri 'hello'
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaabfcc50/initial] (2) [perdir /var/www/html/] rewrite 'hello' -> '/detail.php?keyword=hello'
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaabfcc50/initial] (3) split uri=/detail.php?keyword=hello -> uri=/detail.php, args=keyword=hello
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaabfcc50/initial] (2) [perdir /var/www/html/] trying to replace prefix /var/www/html/ with /
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaabfcc50/initial] (1) [perdir /var/www/html/] internal redirect with /detail.php [INTERNAL REDIRECT]
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaac0e0c0/initial/redir#1] (3) [perdir /var/www/html/] strip per-dir prefix: /var/www/html/detail.php -> detail.php
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaac0e0c0/initial/redir#1] (3) [perdir /var/www/html/] applying pattern '^images/(.*)$' to uri 'detail.php'
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaac0e0c0/initial/redir#1] (3) [perdir /var/www/html/] strip per-dir prefix: /var/www/html/detail.php -> detail.php
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaac0e0c0/initial/redir#1] (3) [perdir /var/www/html/] applying pattern '^search/(.*)/merchant/(.*)/brand/(.*)/page/(.*)$' to uri 'detail.php'
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaac0e0c0/initial/redir#1] (3) [perdir /var/www/html/] strip per-dir prefix: /var/www/html/detail.php -> detail.php
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaac0e0c0/initial/redir#1] (3) [perdir /var/www/html/] applying pattern '^search/(.*)/brand/(.*)/page/(.*)$' to uri 'detail.php'
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaac0e0c0/initial/redir#1] (3) [perdir /var/www/html/] strip per-dir prefix: /var/www/html/detail.php -> detail.php
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaac0e0c0/initial/redir#1] (3) [perdir /var/www/html/] applying pattern '^search/(.*)/merchant/(.*)/page/(.*)$' to uri 'detail.php'
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaac0e0c0/initial/redir#1] (3) [perdir /var/www/html/] strip per-dir prefix: /var/www/html/detail.php -> detail.php
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaac0e0c0/initial/redir#1] (3) [perdir /var/www/html/] applying pattern '^search/(.*)/page/(.*)$' to uri 'detail.php'
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaac0e0c0/initial/redir#1] (3) [perdir /var/www/html/] strip per-dir prefix: /var/www/html/detail.php -> detail.php
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaac0e0c0/initial/redir#1] (3) [perdir /var/www/html/] applying pattern '^search/(.*)/brand/(.*)$' to uri 'detail.php'
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaac0e0c0/initial/redir#1] (3) [perdir /var/www/html/] strip per-dir prefix: /var/www/html/detail.php -> detail.php
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaac0e0c0/initial/redir#1] (3) [perdir /var/www/html/] applying pattern '^search/(.*)/merchant/(.*)$' to uri 'detail.php'
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaac0e0c0/initial/redir#1] (3) [perdir /var/www/html/] strip per-dir prefix: /var/www/html/detail.php -> detail.php
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaac0e0c0/initial/redir#1] (3) [perdir /var/www/html/] applying pattern '^search/(.*)/merchant/(.*)/brand/(.*)$' to uri 'detail.php'
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaac0e0c0/initial/redir#1] (3) [perdir /var/www/html/] strip per-dir prefix: /var/www/html/detail.php -> detail.php
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaac0e0c0/initial/redir#1] (3) [perdir /var/www/html/] applying pattern '^search/(.*)$' to uri 'detail.php'
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaac0e0c0/initial/redir#1] (3) [perdir /var/www/html/] strip per-dir prefix: /var/www/html/detail.php -> detail.php
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaac0e0c0/initial/redir#1] (3) [perdir /var/www/html/] applying pattern '(.*)/productID/(.*)$' to uri 'detail.php'
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaac0e0c0/initial/redir#1] (3) [perdir /var/www/html/] strip per-dir prefix: /var/www/html/detail.php -> detail.php
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaac0e0c0/initial/redir#1] (3) [perdir /var/www/html/] applying pattern '(.*)$' to uri 'detail.php'
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaac0e0c0/initial/redir#1] (2) [perdir /var/www/html/] rewrite 'detail.php' -> '/detail.php?keyword=detail.php'
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaac0e0c0/initial/redir#1] (3) split uri=/detail.php?keyword=detail.php -> uri=/detail.php, args=keyword=detail.php
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaac0e0c0/initial/redir#1] (2) [perdir /var/www/html/] trying to replace prefix /var/www/html/ with /
192.168.0.2 - - [11/Nov/2011:08:54:27 +0200] [website1.co.za/sid#2afe7a520d70][rid#2aaaaac0e0c0/initial/redir#1] (1) [perdir /var/www/html/] internal redirect with /detail.php [INTERNAL REDIRECT]
As you can see, it matches the correct rule and the arguments are setup correctly, but for some reason it then RE-APPLIES that internal redirect again and the argument going through changes from what it should be (which, in this example is "hello") to the file name, detail.php
NO idea how to go about this since everything I know of currently, this should be working because /search works brilliantly.
EDIT
This is what detail.php contains:
<?
echo $_GET["keyword"]."<br>";
echo $_GET["merchant"]."<br>";
echo $_GET["brand"]."<br>";
echo $_GET["page"]."<br>";
echo $_GET["productID"]."<br>";
exit();
?>
The final [L] to /detail.php does an (internal) redirect. And hence Apache sees a totally new request coming in - re-applying the rules ones more.
So options may be to make sure that the rule does not apply to the /detail.php (i.e. by ensuring the argument name is a bit different) or pre-cathcing an ^/detail.php early and negating out.
http://www.sitepoint.com/mod_rewrite-no-endless-loops/ tries to explain this with different words. Another common approach is using things like:
# Loop stopping snippets.
# Uncomment if there are specific pages to exempt.
# RewriteCond %{REQUEST_URI} ^/(stats/|missing\.html|failed_auth\.html|error/).* [NC]
# RewriteRule .* - [L]
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]
where you detect this on the second pass (after the internal redirect) and have it bail in a generic way early.
Dw.
Your indentation does not reflect the semantic association of the RewriteRule and RewriteCond directives.
The conditions
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
only apply if the URI pattern is ^images/([^\?]+)$. What you need is logically flip this and or at least skip the 10 following rules for these to apply for all URI patterns:
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule . - [SKIP=10]
See my article More on using Rewrite rules in .htaccess files for more details.

More Eyeballs on my Poor mod_rewrite Attempt Please

We're changing the way users embed images in their blogs, but I need to make sure previously embedded images are still displayed.
I'm trying to redirect legacy image embed links to either a static image or a single legacy image embed handling file. I'm using mod_rewrite, but it appears I fail. Amazingly, I've managed to cobble together a functional ruleset that prevents hotlinking.
I'm having some difficulty with redirecting legacy embeds to the single legacy handler though. This rule comes under the hotlink set [L], so should only be evaluated if the embed request isn't a result of hotlinking.
Here is my attempt at redirecting all requests to any file like gp[2]?.php[5]?\?p=(.*)
RewriteRule %{REQUEST_FILENAME} gp[2]?\.php[5]?(.*)=(.*)$ [NC]
RewriteRule gp[2]?\.php[5]?(.*)=(.*)$ /legacy/gp.php5$1=$2 [L]
But it doesn't work, I just get my wonderful 404 page.
Some example requests:
http://ecpod.com/includes/gp.php5?p=cfcfbfbe5d9d63eab4a3e784bd30cfe016fcfcfbfbe5d9d6a19ebc57139d56bb
http://www.ecpod.com/includes/gp.php5?p=928e3888a526b462b684aea5609016f928e3888a585d0963ac4ebb335a522e09
http://www.ecpod.com/includes/gp2.php?p=cfcfbfbe5d9d63eab4a3e784bd30cfe016fcfcfbfbe5d9d6a19ebc57139d56bb
What I'm aiming for is to have any request that looks like one of those examples directed to:
http://ecpod.com/legacy/gp.php?p=HASH
Thank you for any help.
Edit:
Legacy urls will have gp|gp2, php|php5 and a query string of n length. They may not necessarily be in the includes directory. It's messy and horrible, I know. Image embeds are handled differently now, this shall never happen again.
Logs:
RewriteRule ^/(includes/)?gp2?\.php5?$ /legacy/gp.php5 [NC,L]
192.168.1.4 - - [06/Sep/2010:14:44:27 +1200] [www.ecpod-dev.dev/sid#2b7d543aa390][rid#2b7d68ec65a8/initial] (3) [perdir /site/http/web/] add path info postfix: /site/http/web/includes -> /site/http/web/includes/gp.php
192.168.1.4 - - [06/Sep/2010:14:44:27 +1200] [www.ecpod-dev.dev/sid#2b7d543aa390][rid#2b7d68ec65a8/initial] (3) [perdir /site/http/web/] strip per-dir prefix: /site/http/web/includes/gp.php -> includes/gp.php
192.168.1.4 - - [06/Sep/2010:14:44:27 +1200] [www.ecpod-dev.dev/sid#2b7d543aa390][rid#2b7d68ec65a8/initial] (3) [perdir /site/http/web/] applying pattern '^/(includes/)?gp2?\.php5?$' to uri 'includes/gp.php'
192.168.1.4 - - [06/Sep/2010:14:44:27 +1200] [www.ecpod-dev.dev/sid#2b7d543aa390][rid#2b7d68ec65a8/initial] (1) [perdir /site/http/web/] pass through /site/http/web/includes
RewriteCond %{QUERY_STRING} (^|&)(p=[^&]+)(&|$) [NC]
RewriteRule ^/(includes/)?gp2?\.php5?$ /legacy/gp.php5?%2 [NC,L]
192.168.1.4 - - [06/Sep/2010:14:43:53 +1200] [www.ecpod-dev.dev/sid#2b8a2ad73400][rid#2b8a2b0ed438/initial] (3) [perdir /site/http/web/] add path info postfix: /site/http/web/includes -> /site/http/web/includes/gp.php
192.168.1.4 - - [06/Sep/2010:14:43:53 +1200] [www.ecpod-dev.dev/sid#2b8a2ad73400][rid#2b8a2b0ed438/initial] (3) [perdir /site/http/web/] strip per-dir prefix: /site/http/web/includes/gp.php -> includes/gp.php
192.168.1.4 - - [06/Sep/2010:14:43:53 +1200] [www.ecpod-dev.dev/sid#2b8a2ad73400][rid#2b8a2b0ed438/initial] (3) [perdir /site/http/web/] applying pattern '^/(includes/)?gp2?\.php5?$' to uri 'includes/gp.php'
192.168.1.4 - - [06/Sep/2010:14:43:53 +1200] [www.ecpod-dev.dev/sid#2b8a2ad73400][rid#2b8a2b0ed438/initial] (1) [perdir /site/http/web/] pass through /site/http/web/includes
RewriteRule /gp2?\.php5?$ /legacy/gp.php5 [NC,L]
192.168.1.4 - - [06/Sep/2010:14:45:37 +1200] [www.ecpod-dev.dev/sid#2b1256a153b0][rid#2b12642705a8/initial] (3) [perdir /site/http/web/] add path info postfix: /site/http/web/includes -> /site/http/web/includes/gp.php
192.168.1.4 - - [06/Sep/2010:14:45:37 +1200] [www.ecpod-dev.dev/sid#2b1256a153b0][rid#2b12642705a8/initial] (3) [perdir /site/http/web/] strip per-dir prefix: /site/http/web/includes/gp.php -> includes/gp.php
192.168.1.4 - - [06/Sep/2010:14:45:37 +1200] [www.ecpod-dev.dev/sid#2b1256a153b0][rid#2b12642705a8/initial] (3) [perdir /site/http/web/] applying pattern '/gp2?\.php5?$' to uri 'includes/gp.php'
192.168.1.4 - - [06/Sep/2010:14:45:37 +1200] [www.ecpod-dev.dev/sid#2b1256a153b0][rid#2b12642705a8/initial] (2) [perdir /site/http/web/] rewrite 'includes/gp.php' -> '/legacy/gp.php5'
192.168.1.4 - - [06/Sep/2010:14:45:37 +1200] [www.ecpod-dev.dev/sid#2b1256a153b0][rid#2b12642705a8/initial] (1) [perdir /site/http/web/] internal redirect with /legacy/gp.php5 [INTERNAL REDIRECT]
192.168.1.4 - - [06/Sep/2010:14:45:37 +1200] [www.ecpod-dev.dev/sid#2b1256a153b0][rid#2b12642756c0/initial/redir#1] (3) [perdir /site/http/web/] strip per-dir prefix: /site/http/web/legacy/gp.php5 -> legacy/gp.php5
192.168.1.4 - - [06/Sep/2010:14:45:37 +1200] [www.ecpod-dev.dev/sid#2b1256a153b0][rid#2b12642756c0/initial/redir#1] (3) [perdir /site/http/web/] applying pattern '/gp2?\.php5?$' to uri 'legacy/gp.php5'
192.168.1.4 - - [06/Sep/2010:14:45:37 +1200] [www.ecpod-dev.dev/sid#2b1256a153b0][rid#2b12642756c0/initial/redir#1] (2) [perdir /site/http/web/] rewrite 'legacy/gp.php5' -> '/legacy/gp.php5'
192.168.1.4 - - [06/Sep/2010:14:45:37 +1200] [www.ecpod-dev.dev/sid#2b1256a153b0][rid#2b12642756c0/initial/redir#1] (1) [perdir /site/http/web/] internal redirect with /legacy/gp.php5 [INTERNAL REDIRECT]
192.168.1.4 - - [06/Sep/2010:14:45:37 +1200] [www.ecpod-dev.dev/sid#2b1256a153b0][rid#2b1264277fa8/initial/redir#2] (3) [perdir /site/http/web/] strip per-dir prefix: /site/http/web/legacy/gp.php5 -> legacy/gp.php5
192.168.1.4 - - [06/Sep/2010:14:45:37 +1200] [www.ecpod-dev.dev/sid#2b1256a153b0][rid#2b1264277fa8/initial/redir#2] (3) [perdir /site/http/web/] applying pattern '/gp2?\.php5?$' to uri 'legacy/gp.php5'
192.168.1.4 - - [06/Sep/2010:14:45:37 +1200] [www.ecpod-dev.dev/sid#2b1256a153b0][rid#2b1264277fa8/initial/redir#2] (2) [perdir /site/http/web/] rewrite 'legacy/gp.php5' -> '/legacy/gp.php5'
192.168.1.4 - - [06/Sep/2010:14:45:37 +1200] [www.ecpod-dev.dev/sid#2b1256a153b0][rid#2b1264277fa8/initial/redir#2] (1) [perdir /site/http/web/] internal redirect with /legacy/gp.php5 [INTERNAL REDIRECT]
192.168.1.4 - - [06/Sep/2010:14:45:37 +1200] [www.ecpod-dev.dev/sid#2b1256a153b0][rid#2b126427a7e8/initial/redir#3] (3) [perdir /site/http/web/] strip per-dir prefix: /site/http/web/legacy/gp.php5 -> legacy/gp.php5
192.168.1.4 - - [06/Sep/2010:14:45:37 +1200] [www.ecpod-dev.dev/sid#2b1256a153b0][rid#2b126427a7e8/initial/redir#3] (3) [perdir /site/http/web/] applying pattern '/gp2?\.php5?$' to uri 'legacy/gp.php5'
192.168.1.4 - - [06/Sep/2010:14:45:37 +1200] [www.ecpod-dev.dev/sid#2b1256a153b0][rid#2b126427a7e8/initial/redir#3] (2) [perdir /site/http/web/] rewrite 'legacy/gp.php5' -> '/legacy/gp.php5'
192.168.1.4 - - [06/Sep/2010:14:45:37 +1200] [www.ecpod-dev.dev/sid#2b1256a153b0][rid#2b126427a7e8/initial/redir#3] (1) [perdir /site/http/web/] internal redirect with /legacy/gp.php5 [INTERNAL REDIRECT]
192.168.1.4 - - [06/Sep/2010:14:45:37 +1200] [www.ecpod-dev.dev/sid#2b1256a153b0][rid#2b126427cfe0/initial/redir#4] (3) [perdir /site/http/web/] strip per-dir prefix: /site/http/web/legacy/gp.php5 -> legacy/gp.php5
192.168.1.4 - - [06/Sep/2010:14:45:37 +1200] [www.ecpod-dev.dev/sid#2b1256a153b0][rid#2b126427cfe0/initial/redir#4] (3) [perdir /site/http/web/] applying pattern '/gp2?\.php5?$' to uri 'legacy/gp.php5'
192.168.1.4 - - [06/Sep/2010:14:45:37 +1200] [www.ecpod-dev.dev/sid#2b1256a153b0][rid#2b126427cfe0/initial/redir#4] (2) [perdir /site/http/web/] rewrite 'legacy/gp.php5' -> '/legacy/gp.php5'
192.168.1.4 - - [06/Sep/2010:14:45:37 +1200] [www.ecpod-dev.dev/sid#2b1256a153b0][rid#2b126427cfe0/initial/redir#4] (1) [perdir /site/http/web/] internal redirect with /legacy/gp.php5 [INTERNAL REDIRECT]
192.168.1.4 - - [06/Sep/2010:14:45:37 +1200] [www.ecpod-dev.dev/sid#2b1256a153b0][rid#2b126427f948/initial/redir#5] (3) [perdir /site/http/web/] strip per-dir prefix: /site/http/web/legacy/gp.php5 -> legacy/gp.php5
192.168.1.4 - - [06/Sep/2010:14:45:37 +1200] [www.ecpod-dev.dev/sid#2b1256a153b0][rid#2b126427f948/initial/redir#5] (3) [perdir /site/http/web/] applying pattern '/gp2?\.php5?$' to uri 'legacy/gp.php5'
192.168.1.4 - - [06/Sep/2010:14:45:37 +1200] [www.ecpod-dev.dev/sid#2b1256a153b0][rid#2b126427f948/initial/redir#5] (2) [perdir /site/http/web/] rewrite 'legacy/gp.php5' -> '/legacy/gp.php5'
192.168.1.4 - - [06/Sep/2010:14:45:37 +1200] [www.ecpod-dev.dev/sid#2b1256a153b0][rid#2b126427f948/initial/redir#5] (1) [perdir /site/http/web/] internal redirect with /legacy/gp.php5 [INTERNAL REDIRECT]
192.168.1.4 - - [06/Sep/2010:14:45:37 +1200] [www.ecpod-dev.dev/sid#2b1256a153b0][rid#2b12642c3048/initial/redir#6] (3) [perdir /site/http/web/] strip per-dir prefix: /site/http/web/legacy/gp.php5 -> legacy/gp.php5
192.168.1.4 - - [06/Sep/2010:14:45:37 +1200] [www.ecpod-dev.dev/sid#2b1256a153b0][rid#2b12642c3048/initial/redir#6] (3) [perdir /site/http/web/] applying pattern '/gp2?\.php5?$' to uri 'legacy/gp.php5'
192.168.1.4 - - [06/Sep/2010:14:45:37 +1200] [www.ecpod-dev.dev/sid#2b1256a153b0][rid#2b12642c3048/initial/redir#6] (2) [perdir /site/http/web/] rewrite 'legacy/gp.php5' -> '/legacy/gp.php5'
192.168.1.4 - - [06/Sep/2010:14:45:37 +1200] [www.ecpod-dev.dev/sid#2b1256a153b0][rid#2b12642c3048/initial/redir#6] (1) [perdir /site/http/web/] internal redirect with /legacy/gp.php5 [INTERNAL REDIRECT]
192.168.1.4 - - [06/Sep/2010:14:45:37 +1200] [www.ecpod-dev.dev/sid#2b1256a153b0][rid#2b12642c5800/initial/redir#7] (3) [perdir /site/http/web/] strip per-dir prefix: /site/http/web/legacy/gp.php5 -> legacy/gp.php5
192.168.1.4 - - [06/Sep/2010:14:45:37 +1200] [www.ecpod-dev.dev/sid#2b1256a153b0][rid#2b12642c5800/initial/redir#7] (3) [perdir /site/http/web/] applying pattern '/gp2?\.php5?$' to uri 'legacy/gp.php5'
192.168.1.4 - - [06/Sep/2010:14:45:37 +1200] [www.ecpod-dev.dev/sid#2b1256a153b0][rid#2b12642c5800/initial/redir#7] (2) [perdir /site/http/web/] rewrite 'legacy/gp.php5' -> '/legacy/gp.php5'
192.168.1.4 - - [06/Sep/2010:14:45:37 +1200] [www.ecpod-dev.dev/sid#2b1256a153b0][rid#2b12642c5800/initial/redir#7] (1) [perdir /site/http/web/] internal redirect with /legacy/gp.php5 [INTERNAL REDIRECT]
192.168.1.4 - - [06/Sep/2010:14:45:37 +1200] [www.ecpod-dev.dev/sid#2b1256a153b0][rid#2b12642c81f0/initial/redir#8] (3) [perdir /site/http/web/] strip per-dir prefix: /site/http/web/legacy/gp.php5 -> legacy/gp.php5
192.168.1.4 - - [06/Sep/2010:14:45:37 +1200] [www.ecpod-dev.dev/sid#2b1256a153b0][rid#2b12642c81f0/initial/redir#8] (3) [perdir /site/http/web/] applying pattern '/gp2?\.php5?$' to uri 'legacy/gp.php5'
192.168.1.4 - - [06/Sep/2010:14:45:37 +1200] [www.ecpod-dev.dev/sid#2b1256a153b0][rid#2b12642c81f0/initial/redir#8] (2) [perdir /site/http/web/] rewrite 'legacy/gp.php5' -> '/legacy/gp.php5'
192.168.1.4 - - [06/Sep/2010:14:45:37 +1200] [www.ecpod-dev.dev/sid#2b1256a153b0][rid#2b12642c81f0/initial/redir#8] (1) [perdir /site/http/web/] internal redirect with /legacy/gp.php5 [INTERNAL REDIRECT]
192.168.1.4 - - [06/Sep/2010:14:45:37 +1200] [www.ecpod-dev.dev/sid#2b1256a153b0][rid#2b12642cad80/initial/redir#9] (3) [perdir /site/http/web/] strip per-dir prefix: /site/http/web/legacy/gp.php5 -> legacy/gp.php5
192.168.1.4 - - [06/Sep/2010:14:45:37 +1200] [www.ecpod-dev.dev/sid#2b1256a153b0][rid#2b12642cad80/initial/redir#9] (3) [perdir /site/http/web/] applying pattern '/gp2?\.php5?$' to uri 'legacy/gp.php5'
192.168.1.4 - - [06/Sep/2010:14:45:37 +1200] [www.ecpod-dev.dev/sid#2b1256a153b0][rid#2b12642cad80/initial/redir#9] (2) [perdir /site/http/web/] rewrite 'legacy/gp.php5' -> '/legacy/gp.php5'
192.168.1.4 - - [06/Sep/2010:14:45:37 +1200] [www.ecpod-dev.dev/sid#2b1256a153b0][rid#2b12642cad80/initial/redir#9] (1) [perdir /site/http/web/] internal redirect with /legacy/gp.php5 [INTERNAL REDIRECT]
192.168.1.4 - - [06/Sep/2010:14:45:37 +1200] [www.ecpod-dev.dev/sid#2b1256a153b0][rid#2b12642cd840/initial/redir#10] (3) [perdir /site/http/web/] strip per-dir prefix: /site/http/web/legacy/gp.php5 -> legacy/gp.php5
192.168.1.4 - - [06/Sep/2010:14:45:37 +1200] [www.ecpod-dev.dev/sid#2b1256a153b0][rid#2b12642cd840/initial/redir#10] (3) [perdir /site/http/web/] applying pattern '/gp2?\.php5?$' to uri 'legacy/gp.php5'
192.168.1.4 - - [06/Sep/2010:14:45:37 +1200] [www.ecpod-dev.dev/sid#2b1256a153b0][rid#2b12642cd840/initial/redir#10] (2) [perdir /site/http/web/] rewrite 'legacy/gp.php5' -> '/legacy/gp.php5'
192.168.1.4 - - [06/Sep/2010:14:45:37 +1200] [www.ecpod-dev.dev/sid#2b1256a153b0][rid#2b12642cd840/initial/redir#10] (1) [perdir /site/http/web/] internal redirect with /legacy/gp.php5 [INTERNAL REDIRECT]
Outside <Directory>:
http://www.ecpod-dev.dev/includes/gp.php?p=ff2287e8bb45572e38304afa184ba1a464016fff2287e8bb45572e0606111e7a
RewriteRule ^/(includes/)?gp2?\.php5?$ /legacy/gp.php5 [NC,L]
192.168.1.4 - - [06/Sep/2010:23:46:54 +1200] [www.ecpod-dev.dev/sid#2ac33a5a83d0][rid#2ac34f0c4608/initial] (1) [perdir /site/http/web/] pass through /site/http/web/includes
RewriteCond %{QUERY_STRING} (^|&)(p=[^&]+)(&|$) [NC]
RewriteRule ^/(includes/)?gp2?\.php5?$ /legacy/gp.php5?%2 [NC,L]
192.168.1.4 - - [06/Sep/2010:23:47:47 +1200] [www.ecpod-dev.dev/sid#2b02af320470][rid#2b02bcb7b688/initial] (1) [perdir /site/http/web/] pass through /site/http/web/includes
RewriteRule /gp2?\.php5?$ /legacy/gp.php5 [NC,L]
192.168.1.4 - - [06/Sep/2010:23:48:45 +1200] [www.ecpod-dev.dev/sid#2aea076a73b0][rid#2aea14f025a8/initial] (1) [perdir /site/http/web/] pass through /site/http/web/includes
The input to the RewriteRule test pattern will not contain the query string, so testing for it will prevent the rule from ever matching and applying.
Taking that into account, we can rework the rule into something like this:
RewriteRule ^(includes/)?gp2?\.php5?$ /legacy/gp.php5 [NC,L]
Assuming that the query string will already be in the format you want, we can just let mod_rewrite append it automatically by not specifying a replacement in our rewrite.
If you aren't sure that the query string will always be just p=HASH, and want to guarantee that it will be for the target script, we can extend the rule block to take that into account, looking for the p parameter in the query string and replacing the existing query string with just that piece:
RewriteCond %{QUERY_STRING} (^|&)(p=[^&]+)(&|$) [NC]
RewriteRule ^(includes/)?gp2?\.php5?$ /legacy/gp.php5?%2 [NC,L]
Per Roland Illig's comment, you could also do the following to be less restrictive on the directory the file is in:
RewriteCond %{REQUEST_URI} !^/legacy/
# Input to RewriteRule in .htaccess never has a leading slash, so make sure
# we're at the beginning or there was a slash before the filename
RewriteRule (^|/)gp2?\.php5?$ /legacy/gp.php5 [NC,L]
Edit: Changed rules to reflect how they would be defined in httpd.conf instead of .htaccess.
Edit: ...And then changed it back again.
This should do it:
RewriteRule ^\/includes\/gp\.php5?\?p=([a-f,0-9]{64})$ /legacy/gp.php?p=$1