I have a PHP file, index.php, that is set as the index file for my site. If someone wants to pull up a value for a, as an example, then they would type http://www.website.com/?value=a. Is it possible to set up a redirect so that the user just has to type in http://www.website.com/a? I tried setting up a redirect from / to /?value=, but the a is being forgotten and the user is simply redirected to www.website.com/?value=. Is this possible to do? Also, what is the difference between a Redirect and a RedirectMatch? Thanks.

I am using Apache on my Mac Snow Leopard Server.

Edit: I found this on Apple's website: you can use a RedirectMatch with a pattern of (.*)\.jpg and a path of www.website.com/$1.jpg to forward all JPG requests to website.com. Any idea how I could get this to work with the PHP?

3 Answers 3


I'm not sure if this is possible with mod_alias, but it certainly can be done with mod_rewrite. As walker suggests, it's probably more user-friendly to do this as an internal rewrite, so that users won't actually see the URL change:

# Include these at the top of your root .htaccess file:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /

# Map any requests for non-existent files or dirs to index.php:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /index.php?value=$1 [QSA]

You may want to replace the .+ bit with a regular expression that more closely matches your set of allowed values; the regexp I used just matches any non-empty string. For example, if your values are all lowercase alphanumeric, you may use [a-z0-9]+ instead.

An alternative solution would be to use FallbackResource from mod_dir, like this:

FallbackResource /index.php

However, you'd then have to modify your index.php script to extract the value from the appropriate $_SERVER variables (which I'd assume to be the same as these ones, although the documentation doesn't seem to actually say so). In older Apache versions, the same effect could be achieved by setting a custom 404 error handler like this:

ErrorDocument 404 /index.php

but then you'd also need to remember to explicitly send a 200 OK status using header() to override the default 404 status code. (Otherwise e.g. search engines wouldn't index your pages, since they'd see the 404 status and assume the content was just an error message.)


You better use Rewrite instead of Redirect. This way the user is not redirected but presented the right data. Do it this way...

RewriteRule ^/([a-zA-Z0-9._])$ /?value=$1 [L,NC]


I set a RedirectMatch with the pattern /(.*) to second.example.com/?a=$1. It can't be redirected back to example.com because that will result in an infinite loop. I don't want to do the RedirectMatch within a specific directory (example.com/a/something) because I want it to be as easy as possible.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.