views:

29

answers:

2

I'm trying to do something like this...

Redirect mysite.com/directory/ To mysite.com/directory/do

But ONLY when "/directory/" is opened without pointing to a file. I am aware that "DirectoryIndex" can help with this, but I want the file's name (which is "do") to appear in the url the users sees. Is there a way to accomplish this with .htaccess..?

Thanks in advance!

+1  A: 

If I've understood correctly, you want something like this:

RewriteEngine On
# Only redirect if we're in /directory/ and *not* pointing at
# a file that already exists in the filesystem
RewriteCond %{REQUEST_URI} ^/directory/
RewriteCond %{REQUEST_URI} !-f
RewriteRule . /directory/do [R,L]

Alternatively, if you meant "when the user accesses the URL mysite.com/directory/ fullstop", which on second thought you may have, this will work as well:

RewriteEngine On
RewriteCond %{REQUEST_URI} ^/directory/$
RewriteRule . /directory/do [R,L]

Edit: In case /do isn't an actual file:

RewriteEngine On

RewriteCond %{REQUEST_URI}  ^/directory/
RewriteCond %{REQUEST_URI} !^/directory/do$
RewriteCond %{REQUEST_URI} !-f
RewriteRule . /directory/do [R,L]
Tim Stone
Weird, I can't get your first code snippet to work. The .htaccess file is in a sub-directory of the root, so I changed "/directory/" in both instances to "/redeem/surveys/" but no dice. Any ideas?
motionman95
I made the mistake of thinking that /do is an actual exists-in-the-filesystem-file, but it could easily not be. If that's the case, see the edit. If other things that "exist" in that directory are also not real files, there's a solution to that too, but admittedly I think there are better ways to solve it using scripting at that point.
Tim Stone
A: 
RewriteEngine On

RewriteCond %{REQUEST_URI} ^/redeem/surveys/$ [NC]
RewriteCond %{REQUEST_URI} !-f
RewriteRule ^(.*)$ /redeem/surveys/do$1 [L,R=301]

That works...but can it be better...?

motionman95