More Eyeballs on my Poor mod_rewrite Attempt Please - apache

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

Related

Difference in .htaccess behaviour

I have finished developing a micro app with Phalcon and tried to move it to production server, but have encountered some problems with .htaccess rules.
Directory layout on the serwer is like this :
/var/www
|- .htaccess
|- redmine/
|- wordpress/
|- (other dirs)
|- MyApp/
|- .htaccess
|- public/
|- .htaccess
|- api.php
/var/www/.htaccess :
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_URI} !(redmine|MyApp) [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
/var/www/MyApp/.htaccess :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} !(\.gif|\.jpg|\.png|\.mp3|\.ods|\.csv|\.xls)$ [NC]
RewriteRule ^$ public/ [L]
RewriteCond %{REQUEST_URI} !(\.gif|\.jpg|\.png|\.mp3|\.ods|\.csv|\.xls)$ [NC]
RewriteRule (.*) public/$1 [L]
</IfModule>
/var/www/MyApp/public/.htaccess :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ api.php?_url=/$1 [QSA,L]
</IfModule>
I'm really new to all this. The top-most .htaccess was not setup by me, and frankly I don't know what is going on in there. The others are based on Phalcon tutorials.
Everything worked on my local machine, but stopped when I uploaded it. I turned on logging in both places.
Local log :
[perdir /Users/Losiowaty/MyApp/] add path info postfix: /Users/Losiowaty/MyApp/api -> /Users/Losiowaty/MyApp/api/poeci
[perdir /Users/Losiowaty/MyApp/] strip per-dir prefix: /Users/Losiowaty/MyApp/api/poeci -> api/poeci
[perdir /Users/Losiowaty/MyApp/] applying pattern '^$' to uri 'api/poeci'
[perdir /Users/Losiowaty/MyApp/] add path info postfix: /Users/Losiowaty/MyApp/api -> /Users/Losiowaty/MyApp/api/poeci
[perdir /Users/Losiowaty/MyApp/] strip per-dir prefix: /Users/Losiowaty/MyApp/api/poeci -> api/poeci
[perdir /Users/Losiowaty/MyApp/] applying pattern '(.*)' to uri 'api/poeci'
[perdir /Users/Losiowaty/MyApp/] RewriteCond: input='/MyApp/api/poeci' pattern='!(\\.gif|\\.jpg|\\.png|\\.mp3|\\.ods|\\.csv|\\.xls)$' [NC] => matched
[perdir /Users/Losiowaty/MyApp/] rewrite 'api/poeci' -> 'public/api/poeci'
[perdir /Users/Losiowaty/MyApp/] add per-dir prefix: public/api/poeci -> /Users/Losiowaty/MyApp/public/api/poeci
[perdir /Users/Losiowaty/MyApp/] strip document_root prefix: /Users/Losiowaty/MyApp/public/api/poeci -> /MyApp/public/api/poeci
[perdir /Users/Losiowaty/MyApp/] internal redirect with /MyApp/public/api/poeci [INTERNAL REDIRECT]
[perdir /Users/Losiowaty/MyApp/public/] add path info postfix: /Users/Losiowaty/MyApp/public/api -> /Users/Losiowaty/MyApp/public/api/poeci
[perdir /Users/Losiowaty/MyApp/public/] strip per-dir prefix: /Users/Losiowaty/MyApp/public/api/poeci -> api/poeci
[perdir /Users/Losiowaty/MyApp/public/] applying pattern '^(.*)$' to uri 'api/poeci'
[perdir /Users/Losiowaty/MyApp/public/] RewriteCond: input='/Users/Losiowaty/MyApp/public/api' pattern='!-d' => matched
[perdir /Users/Losiowaty/MyApp/public/] RewriteCond: input='/Users/Losiowaty/MyApp/public/api' pattern='!-f' => matched
[perdir /Users/Losiowaty/MyApp/public/] rewrite 'api/poeci' -> 'api.php?_url=/api/poeci'
split uri=api.php?_url=/api/poeci -> uri=api.php, args=_url=/api/poeci
[perdir /Users/Losiowaty/MyApp/public/] add per-dir prefix: api.php -> /Users/Losiowaty/MyApp/public/api.php
[perdir /Users/Losiowaty/MyApp/public/] strip document_root prefix: /Users/Losiowaty/MyApp/public/api.php -> /MyApp/public/api.php
[perdir /Users/Losiowaty/MyApp/public/] internal redirect with /MyApp/public/api.php [INTERNAL REDIRECT]
[perdir /Users/Losiowaty/MyApp/public/] strip per-dir prefix: /Users/Losiowaty/MyApp/public/api.php -> api.php
[perdir /Users/Losiowaty/MyApp/public/] applying pattern '^(.*)$' to uri 'api.php'
[perdir /Users/Losiowaty/MyApp/public/] RewriteCond: input='/Users/Losiowaty/MyApp/public/api.php' pattern='!-d' => matched
[perdir /Users/Losiowaty/MyApp/public/] RewriteCond: input='/Users/Losiowaty/MyApp/public/api.php' pattern='!-f' => not-matched
[perdir /Users/Losiowaty/MyApp/public/] pass through /Users/Losiowaty/MyApp/public/api.php
Remote log :
[perdir /var/www/MyApp/] add path info postfix: /var/www/MyApp/api -> /var/www/MyApp/api/poeci
[perdir /var/www/MyApp/] strip per-dir prefix: /var/www/MyApp/api/poeci -> api/poeci
[perdir /var/www/MyApp/] applying pattern '^$' to uri 'api/poeci'
[perdir /var/www/MyApp/] add path info postfix: /var/www/MyApp/api -> /var/www/MyApp/api/poeci
[perdir /var/www/MyApp/] strip per-dir prefix: /var/www/MyApp/api/poeci -> api/poeci
[perdir /var/www/MyApp/] applying pattern '(.*)' to uri 'api/poeci'
[perdir /var/www/MyApp/] RewriteCond: input='/MyApp/api/poeci' pattern='!(\\.gif|\\.jpg|\\.png|\\.mp3|\\.ods|\\.csv|\\.xls)$' [NC] => matched
[perdir /var/www/MyApp/] rewrite 'api/poeci' -> 'public/api/poeci'
[perdir /var/www/MyApp/] add per-dir prefix: public/api/poeci -> /var/www/MyApp/public/api/poeci
[perdir /var/www/MyApp/] strip document_root prefix: /var/www/MyApp/public/api/poeci -> /MyApp/public/api/poeci
[perdir /var/www/MyApp/] internal redirect with /MyApp/public/api/poeci [INTERNAL REDIRECT]
[perdir /var/www/MyApp/public/] add path info postfix: /var/www/MyApp/public/api.php -> /var/www/MyApp/public/api.php/poeci
[perdir /var/www/MyApp/public/] strip per-dir prefix: /var/www/MyApp/public/api.php/poeci -> api.php/poeci
[perdir /var/www/MyApp/public/] applying pattern '^(.*)$' to uri 'api.php/poeci'
[perdir /var/www/MyApp/public/] RewriteCond: input='/var/www/MyApp/public/api.php' pattern='!-d' => matched
[perdir /var/www/MyApp/public/] RewriteCond: input='/var/www/MyApp/public/api.php' pattern='!-f' => not-matched
[perdir /var/www/MyApp/public/] pass through /var/www/MyApp/public/api.php
[perdir /var/www/MyApp/public/] add path info postfix: /var/www/MyApp/public/api.php -> /var/www/MyApp/public/api.php/poeci
[perdir /var/www/MyApp/public/] strip per-dir prefix: /var/www/MyApp/public/api.php/poeci -> api.php/poeci
[perdir /var/www/MyApp/public/] applying pattern '^(.*)$' to uri 'api.php/poeci'
[perdir /var/www/MyApp/public/] RewriteCond: input='/var/www/MyApp/public/api.php' pattern='!-d' => matched
[perdir /var/www/MyApp/public/] RewriteCond: input='/var/www/MyApp/public/api.php' pattern='!-f' => not-matched
[perdir /var/www/MyApp/public/] pass through /var/www/MyApp/public/api.php
[perdir /var/www/] strip per-dir prefix: /var/www/poeci -> poeci
[perdir /var/www/] applying pattern '^index\\.php$' to uri 'poeci'
[perdir /var/www/] strip per-dir prefix: /var/www/poeci -> poeci
[perdir /var/www/] applying pattern '.' to uri 'poeci'
[perdir /var/www/] RewriteCond: input='/poeci' pattern='!(redmine|MyApp)' [NC] => matched
[perdir /var/www/] RewriteCond: input='/var/www/poeci' pattern='!-f' => matched
[perdir /var/www/] RewriteCond: input='/var/www/poeci' pattern='!-d' => matched
[perdir /var/www/] rewrite 'poeci' -> '/index.php'
[perdir /var/www/] trying to replace prefix /var/www/ with /
[perdir /var/www/] internal redirect with /index.php [INTERNAL REDIRECT]
The differences begin with the first line with [perdir (...)] ending with /public/. I don't understand why it adds .php and why the !-f rule doesn't match on the remote server.
Local server :
OSX 10.9.3, MAMP 3.0.4, Apache 2.2.26
Remote server :
Debian GNU/Linux 6.0.9, Apache 2.2.16
I wonder whether this is caused by the additional .htaccess file on the remote, or by the difference in Apache version.
I'd appreciate some explanation as to what's going on and any directions on where to look for answers.
Cheers!
This looks like it may be caused by multiviews. In both htaccess files inside MyApp, add this (above your rules, but anywhere is fine):
Options -Multiviews
Note that because api.php exists, a PATH INFO url like: /MyApp/api.php/something will fail the !-f check.

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/

adding index.cfm to url seems to ignore .htaccess

I'm trying to disallow access to certain folders in my webApp, but there seems to be some exception to the use of index.cfm somewhere...
for this example I'm using just the folllowing mod_rewrite rule:
RewriteRule ^(.*)$ index.cfm [QSA,L]
accessing the following urls with results:
/index.cfm //->/index.cfm naturally
/test/test.html //->/index.cfm
/test/ //->/index.cfm
/test/test.cfm //->/index.cfm
/test/index.cfm //->/test/index.cfm
Note that the last request seems to ignore the rewrite rule, how could that happen?
(All files above are not present in the file system with exception to the index.cfm in the root)
EDIT also, the .htaccess file is placed in the root directory
EDIT 2 I added a RewriteLog to see whats going on, There definitely is a difference but I dont see why...
/test/index.html
[rid#15d1048/initial] (2) init rewrite engine with requested uri /test/index.html
[rid#15d1048/initial] (1) pass through /test/index.html
[rid#15d1048/initial] (3) [perdir C:/my/local/dir/] add path info postfix: C:/my/local/dir/test -> C:/my/local/dir/test/index.html
[rid#15d1048/initial] (3) [perdir C:/my/local/dir/] strip per-dir prefix: C:/my/local/dir/test/index.html -> test/index.html
[rid#15d1048/initial] (3) [perdir C:/my/local/dir/] applying pattern '^(.*)$' to uri 'test/index.html'
[rid#15d1048/initial] (2) [perdir C:/my/local/dir/] rewrite 'test/index.html' -> 'index.cfm'
[rid#15d1048/initial] (3) [perdir C:/my/local/dir/] add per-dir prefix: index.cfm -> C:/my/local/dir/index.cfm
[rid#15d1048/initial] (2) [perdir C:/my/local/dir/] strip document_root prefix: C:/my/local/dir/index.cfm -> /index.cfm
[rid#15d1048/initial] (1) [perdir C:/my/local/dir/] internal redirect with /index.cfm [INTERNAL REDIRECT]
[rid#15cb4c0/initial/redir#1] (2) init rewrite engine with requested uri /index.cfm
[rid#15cb4c0/initial/redir#1] (1) pass through /index.cfm
[rid#15cb4c0/initial/redir#1] (3) [perdir C:/my/local/dir/] strip per-dir prefix: C:/my/local/dir/index.cfm -> index.cfm
[rid#15cb4c0/initial/redir#1] (3) [perdir C:/my/local/dir/] applying pattern '^(.*)$' to uri 'index.cfm'
[rid#15cb4c0/initial/redir#1] (2) [perdir C:/my/local/dir/] rewrite 'index.cfm' -> 'index.cfm'
[rid#15cb4c0/initial/redir#1] (3) [perdir C:/my/local/dir/] add per-dir prefix: index.cfm -> C:/my/local/dir/index.cfm
[rid#15cb4c0/initial/redir#1] (1) [perdir C:/my/local/dir/] initial URL equal rewritten URL: C:/my/local/dir/index.cfm [IGNORING REWRITE]
/test/index.cfm
[rid#15ca1a0/initial] (2) init rewrite engine with requested uri /test/index.cfm
[rid#15ca1a0/initial] (1) pass through /test/index.cfm
[rid#15ca1a0/initial] (3) [perdir C:/my/local/dir/] add path info postfix: C:/my/local/dir/test -> C:/my/local/dir/test/index.cfm
[rid#15ca1a0/initial] (3) [perdir C:/my/local/dir/] strip per-dir prefix: C:/my/local/dir/test/index.cfm -> test/index.cfm
[rid#15ca1a0/initial] (3) [perdir C:/my/local/dir/] applying pattern '^(.*)$' to uri 'test/index.cfm'
[rid#15ca1a0/initial] (2) [perdir C:/my/local/dir/] rewrite 'test/index.cfm' -> 'index.cfm'
[rid#15ca1a0/initial] (3) [perdir C:/my/local/dir/] add per-dir prefix: index.cfm -> C:/my/local/dir/index.cfm
[rid#15ca1a0/initial] (2) [perdir C:/my/local/dir/] strip document_root prefix: C:/my/local/dir/index.cfm -> /index.cfm
[rid#15ca1a0/initial] (1) [perdir C:/my/local/dir/] internal redirect with /index.cfm [INTERNAL REDIRECT]
Not the perfect answer i was looking for but this is what I did to get the same result:
In the .htaccess I add the following line to forward any files not processed by railo:
RewriteCond %{REQUEST_URI} ^/(my|forbidden|folders)
RewriteRule ^(.*)$ /index.cfm/some/page [L]
In the application.cfc I add the following line to forward the files processed by railo:
<cfif Find(listFirst(arguments.targetPage, "/"), "my,forbidden,folders")>
<cfset GetPageContext().Forward("/index.cfm/some/page")>
</cfif>
This way I can show another page whilst not changing the requested url.
I was hoping for a solution just in the .htaccess but it seems because of a bug in the resin / apache connection this cannot work yet.

Apache rewrite parses URL twice unexpectedly

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]