tarta
tarta

Reputation: 13

htaccess redirect from top to subdirectory

I have a web server with the following (simplified) layout:

/
    www/  # will hold html and PHP files (web content)
    doc/  # some documentation
    lib/  # some libraries

I would like to use htaccess to "redirect" every request to www.mydomain.com/page to www.mydomain.com/www/page.php.

I have tried the following:

Options +FollowSymlinks
RewriteEngine On
RewriteRule ^(.*)$ www/$1.php

But it produces a 500 Internal Server Error.

For debugging purposes, I created a www/test.php page which echoes every GET variable, and I modified my .htaccess:

RewriteRule ^(.*)$ www/test.php?page=$1

just to check whether I'm matching the right things. The expected behaviour when performing a request against www.mydomain.com/somepage would be page=somepage, instead I get page=www/get.php.

Why this behaviour? How can I accomplish what I need?

Upvotes: 1

Views: 30

Answers (1)

Amit Verma
Amit Verma

Reputation: 41219

You have to exclude the path you are rewriting to :

RewriteRule ^((?!www).*)$ www/test.php?page=$1 [NC,L]

otherwise you will get an infinite loop error because www/test.php also matches the rewrite pattern (.*)

Upvotes: 1

Related Questions