# [Solved] Apache issue with trailing slashes after rewrite

## MyZelF

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 local path

```
/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.

----------

## MyZelF

Here's how I solved my issue:

```
# 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]
```

----------

## Mad Merlin

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 

```
curl -I foo.mydomain.com/bar/
```

 and 

```
curl -I foo.mydomain.com/bar
```

You could also add something like 

```
RewriteCond %{REQUEST_URI} !^/foo/
```

 so that requests aren't doubly rewritten (syntax untested).

----------

## MyZelF

Thanks for your reply.

I've tried using only this rule:

```
# 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

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

to be rewritten and shown in address bar as

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

and this does not happen anymore using such rule set.

----------

## Mad Merlin

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.

----------

## MyZelF

 *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

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

is rewritten and shown in address bar as 

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

and not as

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

----------

## Mad Merlin

 *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
> ...

 

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.

----------

## MyZelF

 *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.

----------

