You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@httpd.apache.org by Jimmy Thrasibule <th...@gmail.com> on 2012/06/04 20:28:49 UTC
[users@httpd] 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@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org
Re: [users@httpd] mod_rewrite and directory context
Posted by Jimmy Thrasibule <th...@gmail.com>.
> 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@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org
Re: [users@httpd] mod_rewrite and directory context
Posted by Jimmy Thrasibule <th...@gmail.com>.
> 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@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org
Re: [users@httpd] mod_rewrite and directory context
Posted by Igor Cicimov <ic...@gmail.com>.
On Tue, Jun 5, 2012 at 12:42 PM, Eric Covener <co...@gmail.com> wrote:
> On Mon, Jun 4, 2012 at 10:38 PM, Igor Cicimov <ic...@gmail.com> 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@httpd.apache.org
> For additional commands, e-mail: users-help@httpd.apache.org
>
>
Yep, thanks for correcting me Eric.
Igor
Re: [users@httpd] mod_rewrite and directory context
Posted by Eric Covener <co...@gmail.com>.
On Mon, Jun 4, 2012 at 10:38 PM, Igor Cicimov <ic...@gmail.com> 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@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org
Re: [users@httpd] mod_rewrite and directory context
Posted by Igor Cicimov <ic...@gmail.com>.
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" <th...@gmail.com>
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@httpd.apache.org
> For additional commands, e-mail: users-help@httpd.apache.org
>
>