I have a site set up complete with an index, etc. I would like to redirect only traffic coming directly to www.example.com
to www.example.com/foo.php
. However, if a user tries to access www.example.com/index.php
or any of the other existing pages directly, the request should return the appropriate page as normal. I can't seem to wrap my head around these rewrite rules. How would I accomplish this with .htaccess?
views:
11answers:
1
+2
A:
You could likely get away with just changing the DirectoryIndex
to /foo.php
:
DirectoryIndex foo.php
However, if you wanted to use mod_rewrite
to actually rewrite the URL, there are a few different ways to accomplish this, but the clearist is to just check if the requested resource is an empty string (the root was requested, and the input to RewriteRule
in .htaccess
files doesn't contain a leading slash):
RewriteEngine On
RewriteRule ^$ /foo.php
Any request to the server that is not for /
will simply be ignored and routed by Apache as normal.
Tim Stone
2010-09-17 01:44:46
Worked flawlessly and such a quick response. Any advice on how to go about learning these rewrite rules well?
Oren
2010-09-17 01:53:51
@Oren: Awesome. Outside of reading the documentation, many "typical" cases have been asked and answered here, so it's possible to learn a fair bit from the answers I imagine. If you have a local test server, using [`RewriteLog`](http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritelog) with a high `RewriteLogLevel` can help you get a feel for what's going on internally. Most of it's just regular expressions, trickery, and trial and error though, heh, so it takes a bit of getting used to.
Tim Stone
2010-09-17 02:01:49