You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@httpd.apache.org by Gustau Perez <gu...@marfeel.com> on 2017/08/22 07:01:30 UTC

[users@httpd] RewriteRule: Pattern matching and grouping part of the URL expands to its local filesystem path

   Hello everybody,

   I’ve checking all kinds of sources of information so far without success, I hope I didn’t miss anything.

   I have a very simple RewriteRule which should take the requested resource part. What I want to achieve is to prepend an string before that matched path. Something like: 

	RewriteRule   ^(.*)$             http://myserver/special_path/$1 [R=301]

   I’d say that should take the requested resource path and redirect the client to a new location. It does work in some places, but I’d like to use under a conditional <If>. When I do
that, the match is instead expanded to the local filesystem path, if I request the “/“ (root) of the server, the client is being redirected to:

      http://myserver/special_path/var/www/html <http://myserver/special_path/var/www/html>

   Never mind about the DocumentRoot path. Anyway, that’s not the desired behaviour. 

   Perhaps we’re doing something wrong, but what puzzles me is the fact that if move the redirect logic outside the <If> block, then it apparently works fine.

   Best,

   Gustau

—

Marfeel Solutions S.L.
Avda. Josep Tarradellas 20-30, 6th Floor,
08029 Barcelona, Spain


ES: (+34) 93 178 59 50 ext128
US: (+1) 917-341-2540 ext128
UK:  (+44) 207-048-37-28 ext128

https://www.marfeel.com/
Atenea Knowledge Base




Re: [users@httpd] RewriteRule: Pattern matching and grouping part of the URL expands to its local filesystem path

Posted by Luca Toscano <to...@gmail.com>.
Hi Gustau,

2017-08-22 9:01 GMT+02:00 Gustau Perez <gu...@marfeel.com>:

>
>    Hello everybody,
>
>    I’ve checking all kinds of sources of information so far without
> success, I hope I didn’t miss anything.
>
>    I have a very simple RewriteRule which should take the requested
> resource part. What I want to achieve is to prepend an string before that
> matched path. Something like:
>
> RewriteRule   ^(.*)$             http://myserver/special_path/$1 [R=301]
>
>    I’d say that should take the requested resource path and redirect the
> client to a new location. It does work in some places, but I’d like to use
> under a conditional <If>. When I do
> that, the match is instead expanded to the local filesystem path, if I
> request the “/“ (root) of the server, the client is being redirected to:
>
>       http://myserver/special_path/var/www/html
>
>    Never mind about the DocumentRoot path. Anyway, that’s not the desired
> behaviour.
>
>    Perhaps we’re doing something wrong, but what puzzles me is the fact
> that if move the redirect logic outside the <If> block, then it apparently
> works fine.
>

I'd suggest to try Redirect or RedirectMatch for these use cases, they are
less complicated and more straightforward:

https://httpd.apache.org/docs/2.4/mod/mod_alias.html#redirect

As you can see in https://httpd.apache.org/docs/current/mod/core.html#if
the <If> block accepts only directives that are usable in the directory
context (
https://httpd.apache.org/docs/current/mod/directive-dict.html#Context), and
this is a special note in the RewriteRule docs that might clarify what's
happening:

"In per-directory context (Directory and .htaccess), the Pattern is matched
against only a partial path, for example a request of "/app1/index.html"
may result in comparison against "app1/index.html" or "index.html"
depending on where the RewriteRule is defined."

If I am right a note in the docs might clarify any doubts like yours, but
need to double check first :)

Luca