apache redirect - condition not matched correctly - apache

I have the following simple rewrite-statement and conditions in my virtualhost section of Apache (2.2) config
RewriteCond %{REQUEST_URI} !-d
RewriteCond %{REQUEST_URI} !-f
RewriteRule ^([^\.]{2,})$ /bootstrap.php?url=$1
All requests are nicely redirected to the bootstrap.php, execpt if the file exists. That works fine. However, if a directory exists, I'd expect Apache to run the DirectoryIndex for that directory, or show the contents of the directory (whatever setting I choose), but Apache directs the request to the bootstrap.php too. It seems that the first rewriteCond is somehow not correct. What am I doing wrong?
When doing a debug, and trying to load "localhost/img" (which exists) I get the following output
127.0.0.1 - - [14/Jan/2013:20:18:14 +0100] [localhost/sid#4cd5a8][rid#14442c0/initial] (2) init rewrite engine with requested uri /img/
127.0.0.1 - - [14/Jan/2013:20:18:14 +0100] [localhost/sid#4cd5a8][rid#14442c0/initial] (3) applying pattern '^([^\.]{2,})$' to uri '/img/'
127.0.0.1 - - [14/Jan/2013:20:18:14 +0100] [localhost/sid#4cd5a8][rid#14442c0/initial] (4) RewriteCond: input='/img/' pattern='!-d' => matched
127.0.0.1 - - [14/Jan/2013:20:18:14 +0100] [localhost/sid#4cd5a8][rid#14442c0/initial] (4) RewriteCond: input='/img/' pattern='!-f' => matched
127.0.0.1 - - [14/Jan/2013:20:18:14 +0100] [localhost/sid#4cd5a8][rid#14442c0/initial] (2) rewrite '/img/' -> '/bootstrap.php?url=/img/'
127.0.0.1 - - [14/Jan/2013:20:18:14 +0100] [localhost/sid#4cd5a8][rid#14442c0/initial] (3) split uri=/bootstrap.php?url=/img/ -> uri=/bootstrap.php, args=url=/img/
127.0.0.1 - - [14/Jan/2013:20:18:14 +0100] [localhost/sid#4cd5a8][rid#14442c0/initial] (2) local path result: /bootstrap.php
127.0.0.1 - - [14/Jan/2013:20:18:14 +0100] [localhost/sid#4cd5a8][rid#14442c0/initial] (2) prefixed with document_root to C:/webroot/zippyshoot/bootstrap.php
127.0.0.1 - - [14/Jan/2013:20:18:14 +0100] [localhost/sid#4cd5a8][rid#14442c0/initial] (1) go-ahead with C:/webroot/zippyshoot/bootstrap.php [OK]
Works fine when loading localhost/img/logo.png. Output of the debug log is then
127.0.0.1 - - [14/Jan/2013:20:27:56 +0100] [localhost/sid#4cd5a8][rid#237d978/initial] (2) init rewrite engine with requested uri /img/logo.png
127.0.0.1 - - [14/Jan/2013:20:27:56 +0100] [localhost/sid#4cd5a8][rid#237d978/initial] (3) applying pattern '^([^\.]{2,})$' to uri '/img/logo.png'
127.0.0.1 - - [14/Jan/2013:20:27:56 +0100] [localhost/sid#4cd5a8][rid#237d978/initial] (1) pass through /img/logo.png
what am I doing wrong?

I found the answer to the question in this post
http://amandine.aupetit.info/135/apache2-mod_rewrite/
As from Apache 2.2, the rewriteCond is changed. The correct way to write them is now
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-l

Related

How to exclude a URL from HTTPS

I have tried different links to see if it works, however it doesn't seems to work. I have configured https on Redhat EL6. However i wanted to have an exception from one of the IP.
I wanted to have https enabled for all except one IP 192.168.1.1. When i access the URL from the IP 192.168.1.1, it should get redirected or rewritten to http://cab.abc.com and not to https://cab.abc.com
Snip of my /etc/httpd/conf/httpd.conf :
<VirtualHost *:80>
DocumentRoot /opt/app/cr/public
ServerName cab.abc.com
RewriteEngine On
RewriteLog /var/tmp/rewrite.log
RewriteLogLevel 5
RewriteCond %{REMOTE_ADDR} ^192\.168\.1\.1
# RewriteRule .* http://cab.abc.com%{REQUEST_URI} [R]
# RewriteRule ^/(.*) http://cab.abc.com%{REQUEST_URI} [R]
RewriteRule .* http://cab.abc.com [L]
RewriteCond %{THE_REQUEST} ^(.*)
RewriteRule .* https://cab.abc.com%{REQUEST_URI} [R]
# ProxyPass / https://cab.abc.com/
# ProxyPassReverse / https://cab.abc.com/
</VirtualHost>
I have tried multiple options and checked the rewrite.log file :
192.1681.1 - - [21/Apr/2016:20:36:01 +0000] [cab.abc.com/sid#7fe3e21fe2e0][rid#7fe3e24c9b48/initial] (2) rewrite '/' -> 'http://cab.abc.com'
192.1681.1 - - [21/Apr/2016:20:36:01 +0000] [cab.abc.com/sid#7fe3e21fe2e0][rid#7fe3e24c9b48/initial] (2) implicitly forcing redirect (rc=302) with http://cab.abc.com
192.1681.1 - - [21/Apr/2016:20:36:01 +0000] [cab.abc.com/sid#7fe3e21fe2e0][rid#7fe3e24c9b48/initial] (1) escaping http://cab.abc.com for redirect
192.1681.1 - - [21/Apr/2016:20:36:01 +0000] [cab.abc.com/sid#7fe3e21fe2e0][rid#7fe3e24c9b48/initial] (1) redirect to http://cab.abc.com [REDIRECT/302]
192.1681.1 - - [21/Apr/2016:20:36:01 +0000] [cab.abc.com/sid#7fe3e21fe2e0][rid#7fe3e24c9b48/initial] (2) init rewrite engine with requested uri /
192.1681.1 - - [21/Apr/2016:20:36:01 +0000] [cab.abc.com/sid#7fe3e21fe2e0][rid#7fe3e24c9b48/initial] (3) applying pattern '.*' to uri '/'
192.1681.1 - - [21/Apr/2016:20:36:01 +0000] [cab.abc.com/sid#7fe3e21fe2e0][rid#7fe3e24c9b48/initial] (4) RewriteCond: input=‘192.168.1.1’ pattern='^192\\.168\\.1\\.1’ => matched
192.1681.1 - - [21/Apr/2016:20:36:01 +0000] [cab.abc.com/sid#7fe3e21fe2e0][rid#7fe3e24c9b48/initial] (2) rewrite '/' -> 'http://cab.abc.com'
192.1681.1 - - [21/Apr/2016:20:36:01 +0000] [cab.abc.com/sid#7fe3e21fe2e0][rid#7fe3e24c9b48/initial] (2) implicitly forcing redirect (rc=302) with http://cab.abc.com
192.1681.1 - - [21/Apr/2016:20:36:01 +0000] [cab.abc.com/sid#7fe3e21fe2e0][rid#7fe3e24c9b48/initial] (1) escaping http://cab.abc.com for redirect
192.1681.1 - - [21/Apr/2016:20:36:01 +0000] [cab.abc.com/sid#7fe3e21fe2e0][rid#7fe3e24c9b48/initial] (1) redirect to http://cab.abc.com [REDIRECT/302]
192.1681.1 - - [21/Apr/2016:20:36:01 +0000] [cab.abc.com/sid#7fe3e21fe2e0][rid#7fe3e24c5b28/initial] (2) init rewrite engine with requested uri /
192.1681.1 - - [21/Apr/2016:20:36:01 +0000] [cab.abc.com/sid#7fe3e21fe2e0][rid#7fe3e24c5b28/initial] (3) applying pattern '.*' to uri '/'
192.1681.1 - - [21/Apr/2016:20:36:01 +0000] [cab.abc.com/sid#7fe3e21fe2e0][rid#7fe3e24c5b28/initial] (4) RewriteCond: input=‘192.1681.1.’ pattern='^192\\.168\\.1\\.1’ => matched
Requesting your help to exclude an IP address from HTTPs.
Try these rules after clearing browser cache:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{REMOTE_ADDR} !=192.168.1.1
RewriteRule ^ https://cab.abc.com%{REQUEST_URI} [R,L,NE]
RewriteCond %{REMOTE_ADDR} =192.168.1.1
RewriteRule ^ http://cab.abc.com%{REQUEST_URI} [R,L,NE]

mod_rewrite mysterious subreq

Seems like some apache module is interfering with my request uris as it suffixes ".html" to it.
My rewrite log:
172.16.103.1 - - [08/Mar/2012:14:56:33 +0100] [www.example.org/sid#7ff723575b58][rid#7ff724b4fc58/initial] (1) pass through /folder/subfolder/
172.16.103.1 - - [08/Mar/2012:14:56:33 +0100] [www.example.org/sid#7ff723575b58][rid#7ff724b42468/subreq] (3) [perdir /srv/www/html/project/] add path info postfix: /srv/www/html/project/folder/subfolder.html -> /srv/www/html/trustedshops/folder/subfolder.html/
172.16.103.1 - - [08/Mar/2012:14:56:33 +0100] [www.example.org/sid#7ff723575b58][rid#7ff724b42468/subreq] (3) [perdir /srv/www/html/project/] strip per-dir prefix: /srv/www/html/project/folder/subfolder.html/ -> folder/subfolder.html/
This merely happens on our development servers. But It's hard do compare the whole apache config. Any ideas which module could be responsible?
Turn off MultiViews as this generates subrequests
Ditto DirectoryIndex with a list of possiblities.
Use the NS flag on your rewrite rules, or
RewriteCond %{IS_SUBREQ} t
RwriteRule ^ - [L]

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)!)

rewrite URL for PUT request

I changed the way my URL are working on my server.
It is now www.myserver.com/service instead of www.myserver.com/test/service
I have added a RedirectMatch 301 to my Apache conf file to redirect any access to www.myserver.com/test to www.myserver.com/.
I am receiving file to this server via an HTTP PUT at this URL for example :
www.myserver.com/test/service/put/myfile.xml
The server sending the file don't handle the 301 HTTP status code so the files didn't arrived anymore.
Is there a way to rewrite the URL when it is a PUT Request in order to don't miss any file?
Thanks,
Benjamin
UPDATE :
Here is the RewriteLog content after applying this :
RewriteEngine on
RewriteCond %{REQUEST_METHOD} =PUT
RewriteRule ^/test/(.*) /$1 [PT]
log :
XX.XXX.XXX.XXX - - [16/May/2010:06:33:40 +0000] [www.myserver.com/sid#7f378508aa30][rid#7f378538a828/initial] (2) init rewrite engine with requested uri /test/service/put/myfile.xml
XX.XXX.XXX.XXX - - [16/May/2010:06:33:40 +0000] [www.myserver.com/sid#7f378508aa30][rid#7f378538a828/initial] (3) applying pattern '^/test/(.*)' to uri '/test/service/put/myfile.xml'
XX.XXX.XXX.XXX - - [16/May/2010:06:33:40 +0000] [www.myserver.com/sid#7f378508aa30][rid#7f378538a828/initial] (4) RewriteCond: input='PUT' pattern='=PUT' => matched
XX.XXX.XXX.XXX - - [16/May/2010:06:33:40 +0000] [www.myserver.com/sid#7f378508aa30][rid#7f378538a828/initial] (2) rewrite '/test/service/put/myfile.xml' -> '/service/put/myfile.xml'
XX.XXX.XXX.XXX - - [16/May/2010:06:33:40 +0000] [www.myserver.com/sid#7f378508aa30][rid#7f378538a828/initial] (2) forcing '/service/put/myfile.xml' to get passed through to next API URI-to-filename handler
XX.XXX.XXX.XXX - - [16/May/2010:06:33:40 +0000] [www.myserver.com/sid#7f378508aa30][rid#7f3785393858/subreq] (2) init rewrite engine with requested uri /service/put/myfile.xml
XX.XXX.XXX.XXX - - [16/May/2010:06:33:40 +0000] [www.myserver.com/sid#7f378508aa30][rid#7f3785393858/subreq] (3) applying pattern '^/test/(.*)' to uri '/service/put/myfile.xml'
XX.XXX.XXX.XXX - - [16/May/2010:06:33:40 +0000] [www.myserver.com/sid#7f378508aa30][rid#7f3785393858/subreq] (1) pass through /service/put/myfile.xml
UPDATE 2 :
Apache configuration :
RewriteEngine on
RewriteLog "/etc/httpd/logs/rewrite.log"
RewriteLogLevel 9
RewriteCond %{REQUEST_METHOD} =PUT
RewriteRule ^/test/(.*) /$1 [PT]
RedirectMatch 301 ^/test/(.*)$ http://www.myserver.com/$1
AliasMatch ^/style/(.*)?$ "/var/www/test/www/style/$1"
AliasMatch ^/js/(.*)?$ "/var/www/test/www/js/$1"
AliasMatch ^/min/(.*)?$ "/var/www/test/www/min/$1"
AliasMatch ^/downloads/(.*)?$ "/var/www/test/www/downloads/$1"
AliasMatch ^/images/(.*)?$ "/var/www/test/www/images/$1"
AliasMatch ^/chart/(.*)?$ "/var/www/test/www/graphs/$1"
AliasMatch ^(/.*)?$ "/var/www/test/www/index.php$1"
<Directory "/var/www/test">
AllowOverride None
Allow from all
</Directory>
There is, with mod_rewrite:
RewriteEngine on
RewriteCond %{REQUEST_METHOD} =PUT
RewriteRule ^/test/(.*) /$1

Rewrite Url with apache2

I'm experimenting with CodeIgniter PHP framework, this framework works like:
http://localhost:7777/~dhalsim/ci/index.php/blog
So, I tried to remove index.php part from there. So far I do these:
make $config['index_page'] = "index.php"; to $config['index_page'] = "";
make $config['uri_protocol'] = "REQUEST_URI"; from $config['uri_protocol'] = "AUTO";
enable apache mod_rewrite by "a2enmod rewrite"
put a .htaccess file to /ci directory:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
And of course restart apache server
Here is my apache logs with these configurations:
127.0.0.1 - - [17/Jul/2009:02:21:41 +0300] [localhost/sid#7f48e8ad2968][rid#7f48e8e634c8/initial] (3) [perdir /home/dhalsim/public_html/ci/] strip per-dir prefix: /home/dhalsim/public_html/ci/blog -> blog
127.0.0.1 - - [17/Jul/2009:02:21:41 +0300] [localhost/sid#7f48e8ad2968][rid#7f48e8e634c8/initial] (3) [perdir /home/dhalsim/public_html/ci/] applying pattern '^(.*)$' to uri 'blog'
127.0.0.1 - - [17/Jul/2009:02:21:41 +0300] [localhost/sid#7f48e8ad2968][rid#7f48e8e634c8/initial] (4) [perdir /home/dhalsim/public_html/ci/] RewriteCond: input='/~dhalsim/ci/blog' pattern='^system.*' => not-matched
127.0.0.1 - - [17/Jul/2009:02:21:41 +0300] [localhost/sid#7f48e8ad2968][rid#7f48e8e634c8/initial] (3) [perdir /home/dhalsim/public_html/ci/] strip per-dir prefix: /home/dhalsim/public_html/ci/blog -> blog
127.0.0.1 - - [17/Jul/2009:02:21:41 +0300] [localhost/sid#7f48e8ad2968][rid#7f48e8e634c8/initial] (3) [perdir /home/dhalsim/public_html/ci/] applying pattern '^(.*)$' to uri 'blog'
127.0.0.1 - - [17/Jul/2009:02:21:41 +0300] [localhost/sid#7f48e8ad2968][rid#7f48e8e634c8/initial] (4) [perdir /home/dhalsim/public_html/ci/] RewriteCond: input='/home/dhalsim/public_html/ci/blog' pattern='!-f' => matched
127.0.0.1 - - [17/Jul/2009:02:21:41 +0300] [localhost/sid#7f48e8ad2968][rid#7f48e8e634c8/initial] (4) [perdir /home/dhalsim/public_html/ci/] RewriteCond: input='/home/dhalsim/public_html/ci/blog' pattern='!-d' => matched
127.0.0.1 - - [17/Jul/2009:02:21:41 +0300] [localhost/sid#7f48e8ad2968][rid#7f48e8e634c8/initial] (2) [perdir /home/dhalsim/public_html/ci/] rewrite 'blog' -> 'index.php?/blog'
127.0.0.1 - - [17/Jul/2009:02:21:41 +0300] [localhost/sid#7f48e8ad2968][rid#7f48e8e634c8/initial] (3) split uri=index.php?/blog -> uri=index.php, args=/blog
127.0.0.1 - - [17/Jul/2009:02:21:41 +0300] [localhost/sid#7f48e8ad2968][rid#7f48e8e634c8/initial] (3) [perdir /home/dhalsim/public_html/ci/] add per-dir prefix: index.php -> /home/dhalsim/public_html/ci/index.php
127.0.0.1 - - [17/Jul/2009:02:21:41 +0300] [localhost/sid#7f48e8ad2968][rid#7f48e8e634c8/initial] (2) [perdir /home/dhalsim/public_html/ci/] trying to replace prefix /home/dhalsim/public_html/ci/ with /
127.0.0.1 - - [17/Jul/2009:02:21:41 +0300] [localhost/sid#7f48e8ad2968][rid#7f48e8e634c8/initial] (5) strip matching prefix: /home/dhalsim/public_html/ci/index.php -> index.php
127.0.0.1 - - [17/Jul/2009:02:21:41 +0300] [localhost/sid#7f48e8ad2968][rid#7f48e8e634c8/initial] (4) add subst prefix: index.php -> /index.php
127.0.0.1 - - [17/Jul/2009:02:21:41 +0300] [localhost/sid#7f48e8ad2968][rid#7f48e8e634c8/initial] (1) [perdir /home/dhalsim/public_html/ci/] internal redirect with /index.php [INTERNAL REDIRECT]
Here is the result in Firefox:
404 Not Found:
The requested URL /index.php was not found on this server.
So, what should I do (or where am I wrong) to get work these URLs?
http://localhost:7777/~dhalsim/ci/blog/ instead of http://localhost:7777/~dhalsim/ci/index.php/blog/
Here is how i resolve it:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|flash|css|js|robots\.txt|downloads)
RewriteRule ^(.*)$ /index.php/$1 [L]
Then in the config.php file
/*
|--------------------------------------------------------------------------
| Base Site URL
|--------------------------------------------------------------------------
|
| URL to your CodeIgniter root. Typically this will be your base URL,
| WITH a trailing slash:
|
| http://example.com/
|
*/
$config['base_url'] = "http://localhost:7777/~dhalsim/ci/";
/*
|--------------------------------------------------------------------------
| Index File
|--------------------------------------------------------------------------
|
| Typically this will be your index.php file, unless you've renamed it to
| something else. If you are using mod_rewrite to remove the page set this
| variable so that it is blank.
|
*/
$config['index_page'] = "";
UPDATE
In the routes.php file:
//Point to default controller
$route['default_controller'] = "site/home"; //Set to your controller
// Point to the right controller
$routing_array = array(
'blog' => 'site/blog',
); // set to your blog controler
Use the following Rewrite Rule:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(.*)$ /index.php/$1 [NC,L]
In order for this to work you should setup your CodeIgniter app on a different port or use /etc/hosts to give it a simple Vhost (127.0.0.1 ci and http://ci:7777/ with Apache set to use the ci vhost.). Then you can set the base url to / and not worry about it. Otherwise you're going to have to deal with confusing directory issues and mod_rewrite.