Jump to content

Simple example fails. GET variable no longer present when using mod_rewrite


Recommended Posts

Before diving in to a new project, I created a small temporary one just to see how mod_rewrite works and what I need to do before proceeding

 

The server is Ubuntu and I have the following enabled:

sudo a2enmod rewrite

As well as AllowOverride All in the default site, so technically, mod_rewrite should work just fine

 

So let's say this projects is named reviews and will contain reviews of albums, movies, and such. It is also run locally. So the url to the project is

 

http://localhost/reviews/

 

In the project, I created a file called albums.php which displays the specific album asked for, using an unique Id from the database. In other words, the URL looks like this when displaying all the info from album with Id 1:

 

http://localhost/reviews/album.php?id=1

 

In the album.php file, I set a variable with the id with $_GET['id'] and then query the database. As you can tell, I made it as basic as possible just to demonstrate how things work and to more easily "fix" the issue.

What I want to accomplish is this (to start with)

 

http://localhost/reviews/album/1/

 

So for this to work, I created teh necessary .htaccess file in the reviews root directory with the following in it

 

RewriteEngine On

RewriteRule ^album/([0-9]+)/?$ album.php?id=$1 [NC,L]

 

I also tried

RewriteRule ^album/([0-9]+)/ /album.php?id=$1

 

But this does not work. When I query the database using the variable set by $_GET['id'], it fails as the variable is not set at all. This is somewhat logical as the id variable does no longer exists in the URL (there is no album.php?id=1 anymore). The resulting query is therefore

 

SELECT * FROM albums WHERE Id=

 

But this is what I thought the rewrite would take care of. I created an identical project (the structure) as the examples in this tutorial http://www.addedbytes.com/for-beginners/url-rewriting-for-beginners/ but no dice.

 

What is it that I'm missing here? Do I need something else installed on my Ubuntu system?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.