Jonas Gröger
Jonas Gröger

Reputation: 1628

mod_rewrite does not hide subdirectory

On my server I have multiple domains.

RewriteEngine On
RewriteBase /

# 1. Redirect multiple domains to http://domain.de/
RewriteCond %{HTTP_HOST} !^(www\.)?domain\.de [NC]
RewriteRule ^/?(.*) http://domain.de/$1 [L,R,NE]

# 2. Redirect all requests to /subdirectory
RewriteRule ^$ /subdirectory [L]

The 2. rule is working correctly, but it is not hiding the subdirectory in the url nor does it work as intended: a request for http://domain.de/content/image.png returns 404, because the actual file is located in http://domain.de/subdirectory/content/image.png

Furthermore, I have some folders with tools located aside the subdirectory /subdirectory. I want to make sure I can still access them. This is currently working.

Question

How can I make sure, that the request for http://domain.de/content/image.png works?

What I tried

RewriteCond %{REQUEST_URI} !^/subdirectory/
RewriteRule (.*) /subdirectory/$1 [L]

But that just returns error 500 with the following entry in the apache error log: `Request exceeded the limit of 10 internal redirects due to probable configuration error.

Edit

After guidance provided by Ravi Thapliyal, there is (I guess) one thing remaining: Removing the subdirectory from the url.

[user@domain html]$ curl -I domain.de/
HTTP/1.1 301 Moved Permanently
Date: Mon, 21 Oct 2013 12:42:22 GMT
Server: Apache/2.2.22 (Ubuntu)
Location: http://domain.de/subdirectory/index.php
Vary: Accept-Encoding
Content-Type: text/html

This is what gets returned, but I actually want to get the html not a location header which then of course redirects me externally to the subdirectory, which is then visible to the user. Might it have something to do with another .htaccess file from a subdirectory?

Edit2

Seems the problem is related to the typo3 installation behind the subdirectory. The accepted answer works as expected.

Upvotes: 2

Views: 167

Answers (1)

Ravi K Thapliyal
Ravi K Thapliyal

Reputation: 51711

Your first rule should do an external redirect (changing the domain internally won't matter at all)

RewriteCond %{HTTP_HOST} !^(www\.)?domain\.de [NC]
RewriteRule ^/?(.*)$ http://domain.de/$1 [R=301,L,NE]

Your second rule is not required. The new rule would cover root / request as well.

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d [OR]
RewriteCond %{REQUEST_URI} ^/?$
RewriteCond %{REQUEST_URI} !^/subdirectory [NC]
RewriteRule ^(.*)$ /subdirectory/$1 [L]

The two RewriteConds on %{REQUEST_FILENAME} would make sure you can access any files -f or directories -d present outside /subdirectory.


Basically, the condition %{REQUEST_FILENAME} !-d prevents redirection if the URL path points to any existing directory. This prevents a URL like /existing-directory to be redirected to /subdirectory/existing-directory.

But, this could also prevent a root URL / request which is why you received a directory index forbidden error. Hence, the above condition is [OR]'d with %{REQUEST_URI} ^/?$ to allow / to be redirected to /subdirectory as well.

Upvotes: 2

Related Questions