views:

866

answers:

6

I have an existing htaccess that works fine:

RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule (.*) /default.php 
DirectoryIndex index.php /default.php

I wish to modify this so that all urls that start with /test/ go to /test/default.php, while keeping all other URLs with the existing /default.php.

Example: http://www.x.com/hello.php -- > http://www.x.com/default.php Example: http://www.x.com/test/hello.php -- > http://www.x.com/test/default.php

+1  A: 

Put the rule for /test/ in before the rule for everything else, but give it an [L] flag to stop the rewrite rule processing there if it matches.

xahtep
I tried:RewriteEngine OnRewriteCond %{SCRIPT_FILENAME} !-fRewriteCond %{SCRIPT_FILENAME} !-dRewriteRule ^test/.* /test/default.php [L]RewriteCond %{SCRIPT_FILENAME} !-fRewriteCond %{SCRIPT_FILENAME} !-dRewriteRule (.*) /default.phpDirectoryIndex index.php /default.php this didn't work.
A: 

Try this

RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule (/test/)?(.*) $1/default.php 
DirectoryIndex index.php /default.php
Nick Berardi
neither of these work. http://www.x.com/test/hello.php still redirects to /default,php
did you make sure to remove your other rule and use this one?
Nick Berardi
A: 

This should work:

RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule (test/)?(.*) $1default.php [L]
DirectoryIndex index.php /default.php
smazurov
neither of these work. http://www.x.com/test/hello.php still redirects to /default,php
I just tested it in my dev environment and it works for me. My solution shouldn't even redirect to /default.php just default.php, if it redirects to /default.php then it means that it invokes directoryIndex. Disable that rule, perhaps?
smazurov
A: 

Try this.

RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d

RewriteRule /test/.* /test/default.php 
RewriteRule .* /default.php 

DirectoryIndex index.php /default.php

You also don't need the ()'s because your not setting the value as a variable.

Louis W
A: 

In your main folder, use the following .htaccess

RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule .* default.php [L]
DirectoryIndex index.php default.php

In your ./test/ folder (I assume it is a physical folder), copy and paste the same .htaccess file.

First, you didn't need the / at the beginning of default. Apache will always assume it is looking in the same directory as the .htaccess file is located.

Second, Apache looks backwards when looking for an .htaccess file. So anything in ./test/.htaccess will overwrite what's written in ./.htaccess.

Andrew Moore
A: 

Great! working fine for me.