user969124
user969124

Reputation: 23

Create Apache mod rewrite rule to process all request to subdirectory

I want to redirect all incoming requests to a subdirectory, but can't figure out how. I looked here but that did not work for me.

So, if someone visits www.example.com/test, I want it to redirect to: www.example.com/test/subdirectory, but the URL still needs to be www.example.com/test

I also need the parameters to work, for example www.example.com/test/index.php?action=home must redirect to www.example.com/test/subdirectory/index.php?action=home.

I hope someone can help me!

Upvotes: 2

Views: 1567

Answers (2)

regilero
regilero

Reputation: 30496

So in fact what you ask for is not a redirect.

A redirect means the web server will send a new url containing the subdirectory, the browser will then request this new url, and show it in the url bar of the browser.

What you want is to tranparently map an directory structure to a real directory structure which differs. This is what Apache can do easily with the Alias instruction. Mod-rewrite could also do that, with RewriteRules not containing the [R] tag, this would imply an internal rewrite, not a redirect HTTP response. A [QSA] tag would also tell mod-rewrite to keep the current query string parameters on the rewritten url. But Mod-rewrite is not always simple to understand, and your problem is simple enough for a basic Alias, which is almost always included in Apache (more often than mod-rewrite).

this should be ok:

Alias /test /test/subdirectory

Upvotes: 1

Ulrich Palha
Ulrich Palha

Reputation: 9529

Try adding the following to the .htaccess file in the root directory of your site. Query string parameters are passed through by default.

RewriteEngine on
RewriteBase /

#if not an existing file or directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# rewrite to subdirectory
RewriteRule ^(.*)(/[^/]+)?$ /$1/subdirectory/$2 [L,QSA]

Upvotes: 1

Related Questions