You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@httpd.apache.org by yvand <yv...@gmail.com> on 2013/07/09 15:02:39 UTC

[users@httpd] Issue with URL Rewriting

Hi all,

I want to redirect all requests like /icon.png to /pictures/icon.png, if 
the file exists in the pictures folder.
It seems really trivial but I didn't manage to set up.

Here is my .htaccess :
     RewriteEngine On
     RewriteCond /pictures/%{REQUEST_FILENAME} -f
     RewriteRule ^(.+) /pictures/$1 [L]

If I request /pictures/icon.png it works, but if I request /icon.png I 
get a 404 error.

What is wrong with my .htaccess?

Regards.

PS : Sorry for my bad english

--yvand

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


Re: [users@httpd] Issue with URL Rewriting

Posted by Vincenzo D'Amore <v....@gmail.com>.
Hi yvand,

may be because if you request /icon.png the first RewriteCond cannot find
the correct path.
Please try fix the rewritecond with this:

RewriteCond %{DOCUMENT_ROOT}/pictures%{REQUEST_FILENAME} -f




2013/7/9 yvand <yv...@gmail.com>

> Hi all,
>
> I want to redirect all requests like /icon.png to /pictures/icon.png, if
> the file exists in the pictures folder.
> It seems really trivial but I didn't manage to set up.
>
> Here is my .htaccess :
>     RewriteEngine On
>     RewriteCond /pictures/%{REQUEST_FILENAME} -f
>     RewriteRule ^(.+) /pictures/$1 [L]
>
> If I request /pictures/icon.png it works, but if I request /icon.png I get
> a 404 error.
>
> What is wrong with my .htaccess?
>
> Regards.
>
> PS : Sorry for my bad english
>
> --yvand
>
> ------------------------------**------------------------------**---------
> To unsubscribe, e-mail: users-unsubscribe@httpd.**apache.org<us...@httpd.apache.org>
> For additional commands, e-mail: users-help@httpd.apache.org
>
>


-- 
Vincenzo D'Amore
email: v.damore@gmail.com
skype: free.dev
mobile: +39 349 8513251

Re: [users@httpd] Issue with URL Rewriting

Posted by yvand <yv...@gmail.com>.
Thank you Vincenzo D'Amore for helping me.

> It is quite weird... In your log it seems there isn't the rewritecond 
> part...
> Which rewriteloglevel have you enabled?
I used rewriteloglevel 3.
I changed it to 8 and noticed that it doesn't match as I expected !


With this htaccess :
> RewriteEngine On
> RewriteCond /pictures/%{REQUEST_FILENAME} -f
> RewriteRule ^(.+) /pictures/$1 [L]
I got :
> 127.0.0.1 - - [09/Jul/2013:16:04:36 +0200] 
> [localhost/sid#e454e0][rid#10afbb0/initial] (3) [perdir 
> /home/yvand/www/] strip per-dir prefix: /home/yvand/www/icon.png -> 
> icon.png
> 127.0.0.1 - - [09/Jul/2013:16:04:36 +0200] 
> [localhost/sid#e454e0][rid#10afbb0/initial] (3) [perdir 
> /home/yvand/www/] applying pattern '^(.+)' to uri 'icon.png'
> 127.0.0.1 - - [09/Jul/2013:16:04:36 +0200] 
> [localhost/sid#e454e0][rid#10afbb0/initial] (4) [perdir 
> /home/yvand/www/] RewriteCond: 
> input='/pictures//home/yvand/www/icon.png' pattern='-f' => not-matched
> 127.0.0.1 - - [09/Jul/2013:16:04:36 +0200] 
> [localhost/sid#e454e0][rid#10afbb0/initial] (1) [perdir 
> /home/yvand/www/] pass through /home/yvand/www/icon.png


With :
> RewriteEngine On
> RewriteCond %{DOCUMENT_ROOT}/pictures%{REQUEST_FILENAME} -f
> RewriteRule ^(.+) /pictures/$1 [L]
I got :
> 127.0.0.1 - - [09/Jul/2013:16:08:33 +0200] 
> [localhost/sid#e454e0][rid#10afbb0/initial] (3) [perdir 
> /home/yvand/www/] strip per-dir prefix: /home/yvand/www/icon.png -> 
> icon.png
> 127.0.0.1 - - [09/Jul/2013:16:08:33 +0200] 
> [localhost/sid#e454e0][rid#10afbb0/initial] (3) [perdir 
> /home/yvand/www/] applying pattern '^(.+)' to uri 'icon.png'
> 127.0.0.1 - - [09/Jul/2013:16:08:33 +0200] 
> [localhost/sid#e454e0][rid#10afbb0/initial] (4) [perdir 
> /home/yvand/www/] RewriteCond: 
> input='/home/yvand/www/pictures/home/yvand/www/icon.png' pattern='-f' 
> => not-matched
> 127.0.0.1 - - [09/Jul/2013:16:08:33 +0200] 
> [localhost/sid#e454e0][rid#10afbb0/initial] (1) [perdir 
> /home/yvand/www/] pass through /home/yvand/www/icon.png

As Jens pointed out REQUEST_FILENAME contains the whole path. I thought 
not because of "strip per-dir prefix: /home/yvand/www/icon.png -> 
icon.png". Sorry.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


Re: [users@httpd] Issue with URL Rewriting

Posted by yvand <yv...@gmail.com>.
Le 10/07/2013 00:18, Eric Covener a écrit :
>>> RewriteEngine On
>>> RewriteCond %{DOCUMENT_ROOT}/mysite/pictures%{REQUEST_URI} -f
>>> RewriteRule ^(.+) /mysite/pictures/$1 [L]
>> Unfortunately it doesn't work, because %{REQUEST_URI}contains also
>> /mysite...
>> Indeed if I request /mysite/icon.png, the input of RewriteCond is
>>      '/home/yvand/www/mysite/pictures/mysite/icon.png'
>>
>> My question is how can I access to the URI in RewriteCond?
>> When I say URI I do not mean REQUEST_URI but just icon.png in my example,
>> which is the value used by RewriteRule.
> Since you captured it, it's $1 in the RewriteCond.
>
> I think that's the only way to get at the suffix of the path you're
> implicitly comparing against when you do rewrite in htaccess.
Great! Thank you, it works!
I didn't think one instant it was possible to use $1 in RewriteCond..
In my opinion, it is strange that RewriteRule are treated before 
RewriteCond.

--yvand

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


Re: [users@httpd] Issue with URL Rewriting

Posted by Eric Covener <co...@gmail.com>.
>> RewriteEngine On
>> RewriteCond %{DOCUMENT_ROOT}/mysite/pictures%{REQUEST_URI} -f
>> RewriteRule ^(.+) /mysite/pictures/$1 [L]
>
> Unfortunately it doesn't work, because %{REQUEST_URI}contains also
> /mysite...
> Indeed if I request /mysite/icon.png, the input of RewriteCond is
>     '/home/yvand/www/mysite/pictures/mysite/icon.png'
>
> My question is how can I access to the URI in RewriteCond?
> When I say URI I do not mean REQUEST_URI but just icon.png in my example,
> which is the value used by RewriteRule.

Since you captured it, it's $1 in the RewriteCond.

I think that's the only way to get at the suffix of the path you're
implicitly comparing against when you do rewrite in htaccess.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


Re: [users@httpd] Issue with URL Rewriting

Posted by yvand <yv...@gmail.com>.
I got a new question.
Imagine my website is in a subfolder of document root's directory.
For example, document root is /home/yvand/www, and my website is in 
/home/yvand/www/mysite.
How can I redirect URL like /mysite/icon.png to /mysite/pictures/icon.png ?

I tried to adapt .htaccess file so :
> RewriteEngine On
> RewriteCond %{DOCUMENT_ROOT}/mysite/pictures%{REQUEST_URI} -f
> RewriteRule ^(.+) /mysite/pictures/$1 [L]
Unfortunately it doesn't work, because %{REQUEST_URI}contains also 
/mysite...
Indeed if I request /mysite/icon.png, the input of RewriteCond is
     '/home/yvand/www/mysite/pictures/mysite/icon.png'

My question is how can I access to the URI in RewriteCond?
When I say URI I do not mean REQUEST_URI but just icon.png in my 
example, which is the value used by RewriteRule.

--yvand

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


Re: [users@httpd] Issue with URL Rewriting

Posted by yvand <yv...@gmail.com>.
It works fine with this RewriteCond :
     RewriteCond %{DOCUMENT_ROOT}/pictures%{REQUEST_URI} -f

Thank you all for your help!

--yvand

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


Re: [users@httpd] Issue with URL Rewriting

Posted by Vincenzo D'Amore <v....@gmail.com>.
It is quite weird... In your log it seems there isn't the rewritecond
part...
Which rewriteloglevel have you enabled?


2013/7/9 yvand <yv...@gmail.com>

> Thank you Jens-U. for your help.
>
> I enabled log for URL rewriting, here is what I get if I request /icon.png
> (DocumentRoot = /home/yvand/www)
>
> 127.0.0.1 - - [09/Jul/2013:14:41:28 +0200] [localhost/sid#f884e0][rid#**11f2bc0/initial]
> (3) [perdir /home/yvand/www/] strip per-dir prefix:
> /home/yvand/www/icon.png -> icon.png
> 127.0.0.1 - - [09/Jul/2013:14:41:28 +0200] [localhost/sid#f884e0][rid#**11f2bc0/initial]
> (3) [perdir /home/yvand/www/] applying pattern '^(.+)' to uri 'icon.png'
> 127.0.0.1 - - [09/Jul/2013:14:41:28 +0200] [localhost/sid#f884e0][rid#**11f2bc0/initial]
> (1) [perdir /home/yvand/www/] pass through /home/yvand/www/icon.png
> yvand:~/www$
>
> It seems REQUEST_FILENAME refers to icon.png as expected.
> But I don't understand why it doesn't add pictures in the URL
>
> --yvand
>
> Le 09/07/2013 15:39, Jens-U. Mozdzen a écrit :
>
>  Hi yvand,
>>
>> Zitat von yvand <yv...@gmail.com>:
>>
>>> Hi all,
>>>
>>> I want to redirect all requests like /icon.png to /pictures/icon.png, if
>>> the file exists in the pictures folder.
>>> It seems really trivial but I didn't manage to set up.
>>>
>>> Here is my .htaccess :
>>>     RewriteEngine On
>>>     RewriteCond /pictures/%{REQUEST_FILENAME} -f
>>>     RewriteRule ^(.+) /pictures/$1 [L]
>>>
>>> If I request /pictures/icon.png it works, but if I request /icon.png I
>>> get a 404 error.
>>>
>>> What is wrong with my .htaccess?
>>>
>>
>> as you are in .htaccess (and not in global server/vhost context), I'd
>> expect REQUEST_FILENAME to be the full path of the resource. From
>> http://httpd.apache.org/docs/**2.4/mod/mod_rewrite.html#**rewritecond<http://httpd.apache.org/docs/2.4/mod/mod_rewrite.html#rewritecond>:
>>
>> "The full local filesystem path to the file or script matching the
>> request, if this has already been determined by the server at the time
>> REQUEST_FILENAME is referenced."
>>
>> You may want to set up mod_rewrite logging (http://httpd.apache.org/docs/
>> **2.4/mod/mod_rewrite.html#**logging<http://httpd.apache.org/docs/2.4/mod/mod_rewrite.html#logging>)
>> to verify this... the logging is generally *very* helpful in debugging such
>> problems.
>>
>> Regards,
>> Jens
>>
>>
>> ------------------------------**------------------------------**---------
>> To unsubscribe, e-mail: users-unsubscribe@httpd.**apache.org<us...@httpd.apache.org>
>> For additional commands, e-mail: users-help@httpd.apache.org
>>
>>
>
> ------------------------------**------------------------------**---------
> To unsubscribe, e-mail: users-unsubscribe@httpd.**apache.org<us...@httpd.apache.org>
> For additional commands, e-mail: users-help@httpd.apache.org
>
>


-- 
Vincenzo D'Amore
email: v.damore@gmail.com
skype: free.dev
mobile: +39 349 8513251

Re: [users@httpd] Issue with URL Rewriting

Posted by yvand <yv...@gmail.com>.
Thank you Jens-U. for your help.

I enabled log for URL rewriting, here is what I get if I request /icon.png
(DocumentRoot = /home/yvand/www)

127.0.0.1 - - [09/Jul/2013:14:41:28 +0200] 
[localhost/sid#f884e0][rid#11f2bc0/initial] (3) [perdir 
/home/yvand/www/] strip per-dir prefix: /home/yvand/www/icon.png -> icon.png
127.0.0.1 - - [09/Jul/2013:14:41:28 +0200] 
[localhost/sid#f884e0][rid#11f2bc0/initial] (3) [perdir 
/home/yvand/www/] applying pattern '^(.+)' to uri 'icon.png'
127.0.0.1 - - [09/Jul/2013:14:41:28 +0200] 
[localhost/sid#f884e0][rid#11f2bc0/initial] (1) [perdir 
/home/yvand/www/] pass through /home/yvand/www/icon.png
yvand:~/www$

It seems REQUEST_FILENAME refers to icon.png as expected.
But I don't understand why it doesn't add pictures in the URL

--yvand

Le 09/07/2013 15:39, Jens-U. Mozdzen a écrit :
> Hi yvand,
>
> Zitat von yvand <yv...@gmail.com>:
>> Hi all,
>>
>> I want to redirect all requests like /icon.png to /pictures/icon.png, 
>> if the file exists in the pictures folder.
>> It seems really trivial but I didn't manage to set up.
>>
>> Here is my .htaccess :
>>     RewriteEngine On
>>     RewriteCond /pictures/%{REQUEST_FILENAME} -f
>>     RewriteRule ^(.+) /pictures/$1 [L]
>>
>> If I request /pictures/icon.png it works, but if I request /icon.png 
>> I get a 404 error.
>>
>> What is wrong with my .htaccess?
>
> as you are in .htaccess (and not in global server/vhost context), I'd 
> expect REQUEST_FILENAME to be the full path of the resource. From 
> http://httpd.apache.org/docs/2.4/mod/mod_rewrite.html#rewritecond :
>
> "The full local filesystem path to the file or script matching the 
> request, if this has already been determined by the server at the time 
> REQUEST_FILENAME is referenced."
>
> You may want to set up mod_rewrite logging 
> (http://httpd.apache.org/docs/2.4/mod/mod_rewrite.html#logging) to 
> verify this... the logging is generally *very* helpful in debugging 
> such problems.
>
> Regards,
> Jens
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
> For additional commands, e-mail: users-help@httpd.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


Re: [users@httpd] Issue with URL Rewriting

Posted by "Jens-U. Mozdzen" <jm...@nde.ag>.
Hi yvand,

Zitat von yvand <yv...@gmail.com>:
> Hi all,
>
> I want to redirect all requests like /icon.png to  
> /pictures/icon.png, if the file exists in the pictures folder.
> It seems really trivial but I didn't manage to set up.
>
> Here is my .htaccess :
>     RewriteEngine On
>     RewriteCond /pictures/%{REQUEST_FILENAME} -f
>     RewriteRule ^(.+) /pictures/$1 [L]
>
> If I request /pictures/icon.png it works, but if I request /icon.png  
> I get a 404 error.
>
> What is wrong with my .htaccess?

as you are in .htaccess (and not in global server/vhost context), I'd  
expect REQUEST_FILENAME to be the full path of the resource. From  
http://httpd.apache.org/docs/2.4/mod/mod_rewrite.html#rewritecond :

"The full local filesystem path to the file or script matching the  
request, if this has already been determined by the server at the time  
REQUEST_FILENAME is referenced."

You may want to set up mod_rewrite logging  
(http://httpd.apache.org/docs/2.4/mod/mod_rewrite.html#logging) to  
verify this... the logging is generally *very* helpful in debugging  
such problems.

Regards,
Jens


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org