Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
[Solved] Apache issue with trailing slashes after rewrite
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Networking & Security
View previous topic :: View next topic  
Author Message
MyZelF
Bodhisattva
Bodhisattva


Joined: 25 Feb 2003
Posts: 2010
Location: Venice, Italy

PostPosted: Fri Mar 16, 2012 3:28 pm    Post subject: [Solved] Apache issue with trailing slashes after rewrite Reply with quote

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:

Code:
# 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

Code:
foo.mydomain.com
foo.mydomain.com/
foo.mydomain.com/bar/


while

Code:
foo.mydomain.com/bar


fails as it's redirected to local path

Code:
/foo/foo/bar
instead of
Code:
/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.
_________________
"Larry the Cow was a bit frustrated at the current state of Linux Distributions..."


Last edited by MyZelF on Tue Mar 20, 2012 12:31 pm; edited 1 time in total
Back to top
View user's profile Send private message
MyZelF
Bodhisattva
Bodhisattva


Joined: 25 Feb 2003
Posts: 2010
Location: Venice, Italy

PostPosted: Tue Mar 20, 2012 12:27 pm    Post subject: Reply with quote

Here's how I solved my issue:

Code:
# 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]

_________________
"Larry the Cow was a bit frustrated at the current state of Linux Distributions..."
Back to top
View user's profile Send private message
Mad Merlin
Veteran
Veteran


Joined: 09 May 2005
Posts: 1155

PostPosted: Tue Mar 27, 2012 12:28 am    Post subject: Reply with quote

FWIW, this happens because foo.com/bar produces a 301 redirect to foo.com/bar/ (this is true in general for directory accesses that are missing the trailing slash). Apache dutifully complies and applies your rewrite rule after the redirect, and then applies the rewrite again on the redirected url.

With your original rewrite directive, compare the results of
Code:
curl -I foo.mydomain.com/bar/
and
Code:
curl -I foo.mydomain.com/bar


You could also add something like
Code:
RewriteCond %{REQUEST_URI} !^/foo/
so that requests aren't doubly rewritten (syntax untested).
_________________
Game! - Where the stick is mightier than the sword!
Back to top
View user's profile Send private message
MyZelF
Bodhisattva
Bodhisattva


Joined: 25 Feb 2003
Posts: 2010
Location: Venice, Italy

PostPosted: Tue Mar 27, 2012 10:30 am    Post subject: Reply with quote

Thanks for your reply.

I've tried using only this rule:

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


The issue with your suggestion is that I want

Code:
http://www.mydomain.com/foo/bar


to be rewritten and shown in address bar as

Code:
http://www.mydomain.com/bar/


and this does not happen anymore using such rule set.
_________________
"Larry the Cow was a bit frustrated at the current state of Linux Distributions..."
Back to top
View user's profile Send private message
Mad Merlin
Veteran
Veteran


Joined: 09 May 2005
Posts: 1155

PostPosted: Tue Mar 27, 2012 3:48 pm    Post subject: Reply with quote

Your rewrite rules all have "RewriteCond %{HTTP_HOST} ^foo\.mydomain\.com$", I'm not sure how they would apply to www.mydomain.com either way. Unless you meant for a foo.mydomain.com in there somwhere.
_________________
Game! - Where the stick is mightier than the sword!
Back to top
View user's profile Send private message
MyZelF
Bodhisattva
Bodhisattva


Joined: 25 Feb 2003
Posts: 2010
Location: Venice, Italy

PostPosted: Tue Mar 27, 2012 3:55 pm    Post subject: Reply with quote

Mad Merlin wrote:
Your rewrite rules all have "RewriteCond %{HTTP_HOST} ^foo\.mydomain\.com$", I'm not sure how they would apply to www.mydomain.com either way. Unless you meant for a foo.mydomain.com in there somwhere.


Yep, sorry.

The issue with latest rule I've posted is that

Code:
http://foo.mydomain.com/bar


is rewritten and shown in address bar as

Code:
http://foo.mydomain.com/foo/bar/


and not as

Code:
http://foo.mydomain.com/bar/

_________________
"Larry the Cow was a bit frustrated at the current state of Linux Distributions..."
Back to top
View user's profile Send private message
Mad Merlin
Veteran
Veteran


Joined: 09 May 2005
Posts: 1155

PostPosted: Tue Mar 27, 2012 5:35 pm    Post subject: Reply with quote

MyZelF wrote:
Mad Merlin wrote:
Your rewrite rules all have "RewriteCond %{HTTP_HOST} ^foo\.mydomain\.com$", I'm not sure how they would apply to www.mydomain.com either way. Unless you meant for a foo.mydomain.com in there somwhere.


Yep, sorry.

The issue with latest rule I've posted is that

Code:
http://foo.mydomain.com/bar


is rewritten and shown in address bar as

Code:
http://foo.mydomain.com/foo/bar/


and not as

Code:
http://foo.mydomain.com/bar/


Oh, because the rewrite applies to the initial redirect, and so the constructed url that's spit out for the redirect includes the rewrite... that sucks. In that case, a better solution isn't immediately obvious to me.
_________________
Game! - Where the stick is mightier than the sword!
Back to top
View user's profile Send private message
MyZelF
Bodhisattva
Bodhisattva


Joined: 25 Feb 2003
Posts: 2010
Location: Venice, Italy

PostPosted: Tue Mar 27, 2012 9:24 pm    Post subject: Reply with quote

Mad Merlin wrote:
Oh, because the rewrite applies to the initial redirect, and so the constructed url that's spit out for the redirect includes the rewrite... that sucks. In that case, a better solution isn't immediately obvious to me.


I'll keep my proposed rules, although I must agree I cannot find it that elegant!

Thanks for you help.
_________________
"Larry the Cow was a bit frustrated at the current state of Linux Distributions..."
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Networking & Security All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum