Lucas Penney
Lucas Penney

Reputation: 2674

.htaccess Redirect

I need to create a redirect that sends the user to a specified php page with the variable of the page they originally requested, such as:

http://website.com/4

would send them to

http://website.com/download.php?id=4

However I don't want to redirect them if they request an actual page in the root directory, such as website.com/index.php.

Any idea how to accomplish this?

Upvotes: 0

Views: 155

Answers (2)

Matt Stein
Matt Stein

Reputation: 3053

Should be exactly what you asked for; rewrites the URL to include download.php?id= unless the request is for any file that physically exists already:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ download.php?id=$1 [L]

Edit: I added the RewriteEngine On because it may not work without it depending on your server setup. To be fair, mlerley's answer reminded me that it should be there.

Upvotes: 1

mlerley
mlerley

Reputation: 70

Assuming Apache 2.2:

RewriteEngine On
RewriteRule ^(\d+)$ /download.php?id=$1

The url doesn't change in this case. This also assumes that your id is always a number.

Upvotes: 1

Related Questions