Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Apache: Users

mod_rewrite and directory context

 

 

Apache users RSS feed   Index | Next | Previous | View Threaded


thrasibule.jimmy at gmail

Jun 4, 2012, 11:28 AM

Post #1 of 6 (369 views)
Permalink
mod_rewrite and directory context

Hi,

I'm trying to pass everything that is not a file to `index.php` as a
request parameter without using a `.htaccess` file.

A request to http://www.example.com/test must be rewritten to
http://www.example.com/index.php?q=/test.

I'm using Debian Squeeze (6.0) and Apache 2.2.16. Here is my
configuration file:

<VirtualHost *:80>
DocumentRoot /var/www/example
ServerName www.example.com
ServerAlias example.com

ErrorLog /var/log/apache2/error/example.log
CustomLog /var/log/apache2/access/example.log combined

<IfModule mod_rewrite.c>
RewriteEngine On

RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^/(.*) http://www.example.com/$1 [L,R=301]
</IfModule>

<Directory "/var/www/test">
AllowOverride None
Options -Indexes +FollowSymLinks
DirectoryIndex index.php

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^/(.*)$ index.php?q=$1 [L,QSA]
</IfModule>
</Directory>
</VirtualHost>

The first call to `mod_rewrite` within the _VirtualHost_ scope will as
expected change example.com to www.example.com. But, the second call in
the _Directory_ does not seems to work and calling
http://www.example.com/test will throw a 404 error.

Here is the result in the log file:

(2) init rewrite engine with requested uri /test
(3) applying pattern '^/(.*)' to uri '/test'
(4) RewriteCond: input='www.example.com' pattern='!^www\.example\.com
$' [NC] => not-matched
(1) pass through /test
(3) [perdir /var/www/example/] strip per-dir
prefix: /var/www/example/test -> test
(3) [perdir /var/www/example/] applying pattern '^/(.*)$' to uri 'test'
(1) [perdir /var/www/example/] pass through /var/www/test/test
File does not exist: /var/www/test/test

What did I miss?

--
Jimmy



---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe [at] httpd
For additional commands, e-mail: users-help [at] httpd


icicimov at gmail

Jun 4, 2012, 7:38 PM

Post #2 of 6 (356 views)
Permalink
Re: mod_rewrite and directory context [In reply to]

First this is wrong:

Options -Indexes +FollowSymLinks

You should never mix + and - in the Options.

Second if you read mod_rewrite docs you will see that QSA flag appends
something, which is why you have double "test" as rewrite result.

Igor
On Jun 5, 2012 4:29 AM, "Jimmy Thrasibule" <thrasibule.jimmy [at] gmail>
wrote:

> Hi,
>
> I'm trying to pass everything that is not a file to `index.php` as a
> request parameter without using a `.htaccess` file.
>
> A request to http://www.example.com/test must be rewritten to
> http://www.example.com/index.php?q=/test.
>
> I'm using Debian Squeeze (6.0) and Apache 2.2.16. Here is my
> configuration file:
>
> <VirtualHost *:80>
> DocumentRoot /var/www/example
> ServerName www.example.com
> ServerAlias example.com
>
> ErrorLog /var/log/apache2/error/example.log
> CustomLog /var/log/apache2/access/example.log combined
>
> <IfModule mod_rewrite.c>
> RewriteEngine On
>
> RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
> RewriteRule ^/(.*) http://www.example.com/$1 [L,R=301]
> </IfModule>
>
> <Directory "/var/www/test">
> AllowOverride None
> Options -Indexes +FollowSymLinks
> DirectoryIndex index.php
>
> <IfModule mod_rewrite.c>
> RewriteEngine On
> RewriteCond %{REQUEST_FILENAME} !-f
> RewriteCond %{REQUEST_FILENAME} !-d
> RewriteCond %{REQUEST_URI} !=/favicon.ico
> RewriteRule ^/(.*)$ index.php?q=$1 [L,QSA]
> </IfModule>
> </Directory>
> </VirtualHost>
>
> The first call to `mod_rewrite` within the _VirtualHost_ scope will as
> expected change example.com to www.example.com. But, the second call in
> the _Directory_ does not seems to work and calling
> http://www.example.com/test will throw a 404 error.
>
> Here is the result in the log file:
>
> (2) init rewrite engine with requested uri /test
> (3) applying pattern '^/(.*)' to uri '/test'
> (4) RewriteCond: input='www.example.com' pattern='!^www\.example\.com
> $' [NC] => not-matched
> (1) pass through /test
> (3) [perdir /var/www/example/] strip per-dir
> prefix: /var/www/example/test -> test
> (3) [perdir /var/www/example/] applying pattern '^/(.*)$' to uri 'test'
> (1) [perdir /var/www/example/] pass through /var/www/test/test
> File does not exist: /var/www/test/test
>
> What did I miss?
>
> --
> Jimmy
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe [at] httpd
> For additional commands, e-mail: users-help [at] httpd
>
>


covener at gmail

Jun 4, 2012, 7:42 PM

Post #3 of 6 (354 views)
Permalink
Re: mod_rewrite and directory context [In reply to]

On Mon, Jun 4, 2012 at 10:38 PM, Igor Cicimov <icicimov [at] gmail> wrote:
> First this is wrong:
>
> Options -Indexes +FollowSymLinks
>
> You should never mix + and - in the Options.
>

Isn't it just +/- and non +/- that are the mix to avoid?

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe [at] httpd
For additional commands, e-mail: users-help [at] httpd


icicimov at gmail

Jun 4, 2012, 8:59 PM

Post #4 of 6 (356 views)
Permalink
Re: mod_rewrite and directory context [In reply to]

On Tue, Jun 5, 2012 at 12:42 PM, Eric Covener <covener [at] gmail> wrote:

> On Mon, Jun 4, 2012 at 10:38 PM, Igor Cicimov <icicimov [at] gmail> wrote:
> > First this is wrong:
> >
> > Options -Indexes +FollowSymLinks
> >
> > You should never mix + and - in the Options.
> >
>
> Isn't it just +/- and non +/- that are the mix to avoid?
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe [at] httpd
> For additional commands, e-mail: users-help [at] httpd
>
>
Yep, thanks for correcting me Eric.

Igor


thrasibule.jimmy at gmail

Jun 5, 2012, 1:48 AM

Post #5 of 6 (352 views)
Permalink
Re: mod_rewrite and directory context [In reply to]

> Second if you read mod_rewrite docs you will see that QSA flag appends
> something, which is why you have double "test" as rewrite result.

I just mess with the directory names but the output is correct and QSA
is the behavior I want.

Here is the correct logs output:

GET /mypath HTTP/1.1

(2) init rewrite engine with requested uri /mypath
(3) applying pattern '^/(.*)' to uri '/mypath'
(4) RewriteCond: input='www.test.com' pattern='!^www\.test\.com$' [NC]
=> not-matched
(1) pass through /mypath
(3) [perdir /var/www/example/] strip per-dir
prefix: /var/www/example/mypath -> mypath
(3) [perdir /var/www/example/] applying pattern '^/(.*)$' to uri
'mypath'
(1) [perdir /var/www/example/] pass through /var/www/example/mypath

File does not exist: /var/www/example/mypath

So it looks like that `mod_rewrite` is applying the last rule but the
conditions are not tested.



---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe [at] httpd
For additional commands, e-mail: users-help [at] httpd


thrasibule.jimmy at gmail

Jun 5, 2012, 2:01 AM

Post #6 of 6 (351 views)
Permalink
Re: mod_rewrite and directory context [In reply to]

> So it looks like that `mod_rewrite` is applying the last rule but the
> conditions are not tested.

OK, I saw my regrettable error, the regex was not correct due to
directory stripping.

This one is now working:
RewriteRule ^(.+)$ index.php?q=/$1 [QSA,L]

--
Jimmy


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe [at] httpd
For additional commands, e-mail: users-help [at] httpd

Apache users RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.