You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@httpd.apache.org by Nala Gnirut <na...@gmail.com> on 2012/03/16 15:40:26 UTC

[users@httpd] Issue with trailing slashes after rewrite

Hi all,
in a shared hosting with no access to httpd.conf, I'm trying to redirect
subdomains to different document root using mod_rewrite.

I'm using this rule in a .htaccess file placed in DocumentRoot:

# Change document root for foo.mydomain.com
RewriteCond %{SERVER_NAME} foo.mydomain.com
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule .* /foo%{REQUEST_URI} [QSA,L]

This works as expected accessing

foo.mydomain.com
foo.mydomain.com/
foo.mydomain.com/bar/

while

foo.mydomain.com/bar

fails as it's redirected to

/foo/foo/bar instead of /foo/bar

Please note that trailing slashes are automatically added to any rule but
the ones rewritten by this rule.

Where's my fault?

Thanks in advance.

Re: [users@httpd] Issue with trailing slashes after rewrite

Posted by Nala Gnirut <na...@gmail.com>.
It seems like I've managed to make it work like I wanted using this rule:

* * *

# Turn rewrite engine on
RewriteEngine On

# Fix missing trailing slashes for foo.mydomain.com
RewriteCond %{HTTP_HOST} ^foo\.mydomain\.com$ [NC]
RewriteCond %{DOCUMENT_ROOT}/foo%{REQUEST_URI}/ -d
RewriteRule [^/]$ %{REQUEST_URI}/ [R=301,L]

# Change DocumentRoot for foo.mydomain.com
RewriteCond %{HTTP_HOST} ^foo\.mydomain\.com$
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule .* /foo%{REQUEST_URI}  [QSA,L]

* * *

Thanks everyone for your help.

On Sun, Mar 18, 2012 at 11:36 PM, Nala Gnirut <na...@gmail.com> wrote:

> On Sun, Mar 18, 2012 at 11:16 PM, Eric Covener <co...@gmail.com> wrote:
>
>> The P flag is explicitly used to proxy. If you want to redirect,
>> substitute a full URL and use the R flag instead.
>>
>
> I need to dynamically change DocumentRoot for some subdomains pointing to
> the same local path.
>
> This rule works (almost) as expected, but has an issue with links to
> subfolders without trailing slash:
>
> RewriteCond %{HTTP_HOST} ^foo\.domain\.com$
>
> RewriteCond %{ENV:REDIRECT_STATUS} ^$
> RewriteRule .* /foo%{REQUEST_URI}  [QSA,L]
>
> That is
>
> http://foo.domain.com/bar/ works, while
>
> http://foo.domain.com/bar is redirected to a wrong local path
> (/foo/foo/bar instead of /foo/bar)
>

Re: [users@httpd] Issue with trailing slashes after rewrite

Posted by Nala Gnirut <na...@gmail.com>.
On Sun, Mar 18, 2012 at 11:16 PM, Eric Covener <co...@gmail.com> wrote:

> The P flag is explicitly used to proxy. If you want to redirect,
> substitute a full URL and use the R flag instead.
>

I need to dynamically change DocumentRoot for some subdomains pointing to
the same local path.

This rule works (almost) as expected, but has an issue with links to
subfolders without trailing slash:

RewriteCond %{HTTP_HOST} ^foo\.domain\.com$
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule .* /foo%{REQUEST_URI}  [QSA,L]

That is

http://foo.domain.com/bar/ works, while

http://foo.domain.com/bar is redirected to a wrong local path (/foo/foo/bar
instead of /foo/bar)

Re: [users@httpd] Issue with trailing slashes after rewrite

Posted by Eric Covener <co...@gmail.com>.
On Sun, Mar 18, 2012 at 5:56 PM, Nala Gnirut <na...@gmail.com> wrote:
> After adapting to my folder structure it should look like
>
> RewriteCond %{HTTP_HOST} ^(.*)\.(.*)\.(.*)$
> RewriteRule ^/(.*) /%1/$1 [P]
>
> Unfortunately neither your original suggestion nor my version seem to cause
> any redirection.

The P flag is explicitly used to proxy. If you want to redirect,
substitute a full URL and use the R flag instead.

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


Re: [users@httpd] Issue with trailing slashes after rewrite

Posted by Nala Gnirut <na...@gmail.com>.
After adapting to my folder structure it should look like

RewriteCond %{HTTP_HOST} ^(.*)\.(.*)\.(.*)$
RewriteRule ^/(.*) /%1/$1 [P]

Unfortunately neither your original suggestion nor my version seem to cause
any redirection.

On Sat, Mar 17, 2012 at 3:09 PM, Igor Cicimov <ic...@gmail.com> wrote:

> Correction
>
>
> RewriteCond %{HTTP_HOST} ^(.*)\.(.*)\.com$
> RewriteRule ^/(.*) /%2/%1/$1 [P,L]
>
>

Re: [users@httpd] Issue with trailing slashes after rewrite

Posted by Igor Cicimov <ic...@gmail.com>.
Correction

RewriteCond %{HTTP_HOST} ^(.*)\.(.*)\.com$
RewriteRule ^/(.*) /%2/%1/$1 [P,L]


On Sun, Mar 18, 2012 at 1:05 AM, Igor Cicimov <ic...@gmail.com> wrote:

> RewriteCond %{HTTP_HOST} ^(.*)\.(.*)\.com$
> RewriteRule ^/(.*) /%2/%1 [P,L]
>  On Mar 18, 2012 12:58 AM, "Nala Gnirut" <na...@gmail.com> wrote:
>
>> Thanks, for the info. Unfortunately the suggested rule does not seem to
>> work as expected.
>>
>> Let me further explain what I'm trying to achieve:
>>
>> All *.mydomain.com subdomains point to the same /mydomain/ local path as
>> DocumentRoot (can't change this behavior due to shared hosting
>> restrictions) and I'd like to have
>>
>> a.mydomain.com -> /mydomain/a (http://a.mydomain.com still shown in
>> address bar)
>> a.mydomain.com/dir1/ -> /mydomain/dir1 (http://a.mydomain.com/dir1/still shown in address bar)
>> b.mydomain.com -> /mydomain/b (http://b.mydomain.com still shown in
>> address bar)
>> b.mydomain.com/dir2/ -> /mydomain/dir2 (http://a.mydomain.com/dir2/still shown in address bar)
>>
>> My rule seems to work this way, with the notably exception of links to
>> subdirs not containing trailing slash
>>
>> Thanks in advance.
>>
>> On Sat, Mar 17, 2012 at 4:03 AM, Igor Cicimov <ic...@gmail.com> wrote:
>>
>>> RewriteCond %{HTTP_HOST} ^foo\.mydomain\.com$ <http://foo.mydomain.com/>
>>>  RewriteCond %{ENV:REDIRECT_STATUS} ^$
>>> RewriteRule ^/(.*) /foo/$1 [L]
>>>
>>> sorry missed the ^ above.
>>>
>>>
>>> On Sat, Mar 17, 2012 at 1:49 PM, Igor Cicimov <ic...@gmail.com>wrote:
>>>
>>>> First SERVER_NAME is apache internal NOT a http header sent with the
>>>> request thus will match ANY request. Use HTTP_HOST instead. You also need
>>>> to escape the dots in the host name.
>>>>
>>>> Second, from the documentation:
>>>>
>>>> "To combine new and old query strings, use the [QSA] flag."
>>>>
>>>> so by using QSA you are modifying the query string adding another foo
>>>> to it thus the result you are seeing.
>>>>
>>>> Finally, your rules should look like:
>>>>
>>>> RewriteCond %{HTTP_HOST} ^foo\.mydomain\.com$<http://foo.mydomain.com/>
>>>>
>>>> RewriteCond %{ENV:REDIRECT_STATUS} ^$
>>>> RewriteRule /(.*) /foo/$1 [L]
>>>>
>>>> Igor
>>>>
>>>>
>>>> On Sat, Mar 17, 2012 at 1:40 AM, Nala Gnirut <na...@gmail.com>wrote:
>>>>
>>>>> Hi all,
>>>>> in a shared hosting with no access to httpd.conf, I'm trying to
>>>>> redirect subdomains to different document root using mod_rewrite.
>>>>>
>>>>> I'm using this rule in a .htaccess file placed in DocumentRoot:
>>>>>
>>>>> # Change document root for foo.mydomain.com
>>>>> RewriteCond %{SERVER_NAME} foo.mydomain.com
>>>>> RewriteCond %{ENV:REDIRECT_STATUS} ^$
>>>>> RewriteRule .* /foo%{REQUEST_URI} [QSA,L]
>>>>>
>>>>> This works as expected accessing
>>>>>
>>>>> foo.mydomain.com
>>>>> foo.mydomain.com/
>>>>> foo.mydomain.com/bar/
>>>>>
>>>>> while
>>>>>
>>>>> foo.mydomain.com/bar
>>>>>
>>>>> fails as it's redirected to
>>>>>
>>>>> /foo/foo/bar instead of /foo/bar
>>>>>
>>>>> Please note that trailing slashes are automatically added to any rule
>>>>> but the ones rewritten by this rule.
>>>>>
>>>>> Where's my fault?
>>>>>
>>>>> Thanks in advance.
>>>>>
>>>>>
>>>>
>>>
>>

Re: [users@httpd] Issue with trailing slashes after rewrite

Posted by Igor Cicimov <ic...@gmail.com>.
RewriteCond %{HTTP_HOST} ^(.*)\.(.*)\.com$
RewriteRule ^/(.*) /%2/%1 [P,L]
 On Mar 18, 2012 12:58 AM, "Nala Gnirut" <na...@gmail.com> wrote:

> Thanks, for the info. Unfortunately the suggested rule does not seem to
> work as expected.
>
> Let me further explain what I'm trying to achieve:
>
> All *.mydomain.com subdomains point to the same /mydomain/ local path as
> DocumentRoot (can't change this behavior due to shared hosting
> restrictions) and I'd like to have
>
> a.mydomain.com -> /mydomain/a (http://a.mydomain.com still shown in
> address bar)
> a.mydomain.com/dir1/ -> /mydomain/dir1 (http://a.mydomain.com/dir1/ still
> shown in address bar)
> b.mydomain.com -> /mydomain/b (http://b.mydomain.com still shown in
> address bar)
> b.mydomain.com/dir2/ -> /mydomain/dir2 (http://a.mydomain.com/dir2/ still
> shown in address bar)
>
> My rule seems to work this way, with the notably exception of links to
> subdirs not containing trailing slash
>
> Thanks in advance.
>
> On Sat, Mar 17, 2012 at 4:03 AM, Igor Cicimov <ic...@gmail.com> wrote:
>
>> RewriteCond %{HTTP_HOST} ^foo\.mydomain\.com$ <http://foo.mydomain.com/>
>>  RewriteCond %{ENV:REDIRECT_STATUS} ^$
>> RewriteRule ^/(.*) /foo/$1 [L]
>>
>> sorry missed the ^ above.
>>
>>
>> On Sat, Mar 17, 2012 at 1:49 PM, Igor Cicimov <ic...@gmail.com> wrote:
>>
>>> First SERVER_NAME is apache internal NOT a http header sent with the
>>> request thus will match ANY request. Use HTTP_HOST instead. You also need
>>> to escape the dots in the host name.
>>>
>>> Second, from the documentation:
>>>
>>> "To combine new and old query strings, use the [QSA] flag."
>>>
>>> so by using QSA you are modifying the query string adding another foo to
>>> it thus the result you are seeing.
>>>
>>> Finally, your rules should look like:
>>>
>>> RewriteCond %{HTTP_HOST} ^foo\.mydomain\.com$ <http://foo.mydomain.com/>
>>>
>>> RewriteCond %{ENV:REDIRECT_STATUS} ^$
>>> RewriteRule /(.*) /foo/$1 [L]
>>>
>>> Igor
>>>
>>>
>>> On Sat, Mar 17, 2012 at 1:40 AM, Nala Gnirut <na...@gmail.com>wrote:
>>>
>>>> Hi all,
>>>> in a shared hosting with no access to httpd.conf, I'm trying to
>>>> redirect subdomains to different document root using mod_rewrite.
>>>>
>>>> I'm using this rule in a .htaccess file placed in DocumentRoot:
>>>>
>>>> # Change document root for foo.mydomain.com
>>>> RewriteCond %{SERVER_NAME} foo.mydomain.com
>>>> RewriteCond %{ENV:REDIRECT_STATUS} ^$
>>>> RewriteRule .* /foo%{REQUEST_URI} [QSA,L]
>>>>
>>>> This works as expected accessing
>>>>
>>>> foo.mydomain.com
>>>> foo.mydomain.com/
>>>> foo.mydomain.com/bar/
>>>>
>>>> while
>>>>
>>>> foo.mydomain.com/bar
>>>>
>>>> fails as it's redirected to
>>>>
>>>> /foo/foo/bar instead of /foo/bar
>>>>
>>>> Please note that trailing slashes are automatically added to any rule
>>>> but the ones rewritten by this rule.
>>>>
>>>> Where's my fault?
>>>>
>>>> Thanks in advance.
>>>>
>>>>
>>>
>>
>

Re: [users@httpd] Issue with trailing slashes after rewrite

Posted by Nala Gnirut <na...@gmail.com>.
Thanks, for the info. Unfortunately the suggested rule does not seem to
work as expected.

Let me further explain what I'm trying to achieve:

All *.mydomain.com subdomains point to the same /mydomain/ local path as
DocumentRoot (can't change this behavior due to shared hosting
restrictions) and I'd like to have

a.mydomain.com -> /mydomain/a (http://a.mydomain.com still shown in address
bar)
a.mydomain.com/dir1/ -> /mydomain/dir1 (http://a.mydomain.com/dir1/ still
shown in address bar)
b.mydomain.com -> /mydomain/b (http://b.mydomain.com still shown in address
bar)
b.mydomain.com/dir2/ -> /mydomain/dir2 (http://a.mydomain.com/dir2/ still
shown in address bar)

My rule seems to work this way, with the notably exception of links to
subdirs not containing trailing slash

Thanks in advance.

On Sat, Mar 17, 2012 at 4:03 AM, Igor Cicimov <ic...@gmail.com> wrote:

> RewriteCond %{HTTP_HOST} ^foo\.mydomain\.com$ <http://foo.mydomain.com/>
> RewriteCond %{ENV:REDIRECT_STATUS} ^$
> RewriteRule ^/(.*) /foo/$1 [L]
>
> sorry missed the ^ above.
>
>
> On Sat, Mar 17, 2012 at 1:49 PM, Igor Cicimov <ic...@gmail.com> wrote:
>
>> First SERVER_NAME is apache internal NOT a http header sent with the
>> request thus will match ANY request. Use HTTP_HOST instead. You also need
>> to escape the dots in the host name.
>>
>> Second, from the documentation:
>>
>> "To combine new and old query strings, use the [QSA] flag."
>>
>> so by using QSA you are modifying the query string adding another foo to
>> it thus the result you are seeing.
>>
>> Finally, your rules should look like:
>>
>> RewriteCond %{HTTP_HOST} ^foo\.mydomain\.com$ <http://foo.mydomain.com/>
>>
>> RewriteCond %{ENV:REDIRECT_STATUS} ^$
>> RewriteRule /(.*) /foo/$1 [L]
>>
>> Igor
>>
>>
>> On Sat, Mar 17, 2012 at 1:40 AM, Nala Gnirut <na...@gmail.com>wrote:
>>
>>> Hi all,
>>> in a shared hosting with no access to httpd.conf, I'm trying to redirect
>>> subdomains to different document root using mod_rewrite.
>>>
>>> I'm using this rule in a .htaccess file placed in DocumentRoot:
>>>
>>> # Change document root for foo.mydomain.com
>>> RewriteCond %{SERVER_NAME} foo.mydomain.com
>>> RewriteCond %{ENV:REDIRECT_STATUS} ^$
>>> RewriteRule .* /foo%{REQUEST_URI} [QSA,L]
>>>
>>> This works as expected accessing
>>>
>>> foo.mydomain.com
>>> foo.mydomain.com/
>>> foo.mydomain.com/bar/
>>>
>>> while
>>>
>>> foo.mydomain.com/bar
>>>
>>> fails as it's redirected to
>>>
>>> /foo/foo/bar instead of /foo/bar
>>>
>>> Please note that trailing slashes are automatically added to any rule
>>> but the ones rewritten by this rule.
>>>
>>> Where's my fault?
>>>
>>> Thanks in advance.
>>>
>>>
>>
>

Re: [users@httpd] Issue with trailing slashes after rewrite

Posted by Igor Cicimov <ic...@gmail.com>.
RewriteCond %{HTTP_HOST} ^foo\.mydomain\.com$ <http://foo.mydomain.com/>
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^/(.*) /foo/$1 [L]

sorry missed the ^ above.

On Sat, Mar 17, 2012 at 1:49 PM, Igor Cicimov <ic...@gmail.com> wrote:

> First SERVER_NAME is apache internal NOT a http header sent with the
> request thus will match ANY request. Use HTTP_HOST instead. You also need
> to escape the dots in the host name.
>
> Second, from the documentation:
>
> "To combine new and old query strings, use the [QSA] flag."
>
> so by using QSA you are modifying the query string adding another foo to
> it thus the result you are seeing.
>
> Finally, your rules should look like:
>
> RewriteCond %{HTTP_HOST} ^foo\.mydomain\.com$ <http://foo.mydomain.com/>
>
> RewriteCond %{ENV:REDIRECT_STATUS} ^$
> RewriteRule /(.*) /foo/$1 [L]
>
> Igor
>
>
> On Sat, Mar 17, 2012 at 1:40 AM, Nala Gnirut <na...@gmail.com>wrote:
>
>> Hi all,
>> in a shared hosting with no access to httpd.conf, I'm trying to redirect
>> subdomains to different document root using mod_rewrite.
>>
>> I'm using this rule in a .htaccess file placed in DocumentRoot:
>>
>> # Change document root for foo.mydomain.com
>> RewriteCond %{SERVER_NAME} foo.mydomain.com
>> RewriteCond %{ENV:REDIRECT_STATUS} ^$
>> RewriteRule .* /foo%{REQUEST_URI} [QSA,L]
>>
>> This works as expected accessing
>>
>> foo.mydomain.com
>> foo.mydomain.com/
>> foo.mydomain.com/bar/
>>
>> while
>>
>> foo.mydomain.com/bar
>>
>> fails as it's redirected to
>>
>> /foo/foo/bar instead of /foo/bar
>>
>> Please note that trailing slashes are automatically added to any rule but
>> the ones rewritten by this rule.
>>
>> Where's my fault?
>>
>> Thanks in advance.
>>
>>
>

Re: [users@httpd] Issue with trailing slashes after rewrite

Posted by Igor Cicimov <ic...@gmail.com>.
First SERVER_NAME is apache internal NOT a http header sent with the
request thus will match ANY request. Use HTTP_HOST instead. You also need
to escape the dots in the host name.

Second, from the documentation:

"To combine new and old query strings, use the [QSA] flag."

so by using QSA you are modifying the query string adding another foo to it
thus the result you are seeing.

Finally, your rules should look like:

RewriteCond %{HTTP_HOST} ^foo\.mydomain\.com$ <http://foo.mydomain.com/>
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule /(.*) /foo/$1 [L]

Igor

On Sat, Mar 17, 2012 at 1:40 AM, Nala Gnirut <na...@gmail.com> wrote:

> Hi all,
> in a shared hosting with no access to httpd.conf, I'm trying to redirect
> subdomains to different document root using mod_rewrite.
>
> I'm using this rule in a .htaccess file placed in DocumentRoot:
>
> # Change document root for foo.mydomain.com
> RewriteCond %{SERVER_NAME} foo.mydomain.com
> RewriteCond %{ENV:REDIRECT_STATUS} ^$
> RewriteRule .* /foo%{REQUEST_URI} [QSA,L]
>
> This works as expected accessing
>
> foo.mydomain.com
> foo.mydomain.com/
> foo.mydomain.com/bar/
>
> while
>
> foo.mydomain.com/bar
>
> fails as it's redirected to
>
> /foo/foo/bar instead of /foo/bar
>
> Please note that trailing slashes are automatically added to any rule but
> the ones rewritten by this rule.
>
> Where's my fault?
>
> Thanks in advance.
>
>