URL rewrite syntax

I’ve just been having a look at this, and have gotten halfway to what I would like to do.

So I’ve got it working by changing my .htaccess file to include:

Options +FollowSymLinks
RewriteEngine on
RewriteRule index/LodgeID/(.*)/ index.php?LodgeID=$1
RewriteRule index/LodgeID/(.*) index.php?LodgeID=$1

Which, for example, is rewriting:

http://lodges.goodsafariguide.com/lodgedetails/index.php?LodgeID=288

To:

http://lodges.goodsafariguide.com/lodgedetails/index/LodgeID/288/

And I’ve resolved the issue of my paths to images and CSS files going AWOL.

What I would like to be able to do is include a variant of the lodge name in the URL.

I do have a field in the lodges table which has a short name string which could be used, e.g.:

africanhorsebacksafaris

Is it possible to use rewrite the URL to:

http://lodges.goodsafariguide.com/[i]lodgename[/i]/

Where ‘lodgename’ is stored as a field in the same lodges table as LodgeID?

Hope that makes sense. Thanks.

I may have been overthinking it.

At the moment the recordset is:

SELECT *
FROM lodges
WHERE LodgeID=ParamLodgeID

Where ParamLodgeID is:

Type: Integer
Default Value: -1
Run-time Value: $_GET[‘LodgeID’]

So I changed the recordset to:

SELECT *
FROM lodges
WHERE Lodge=ParamLodgename

Type: Text
Default Value: -1
Run-time Value: $_GET[‘Lodgename’]

It was a case of sucking it to see, but I wasn’t sure about the default value being -1 for text rather than an integer.

And I added:


RewriteRule index/GSG_URL/(.*)/ index.php?GSG_URL=$1
RewriteRule index/GSG_URL/(.*) index.php?GSG_URL=$1
RewriteRule index/(.*)/ index.php?GSG_URL=$1
RewriteRule index/(.*) index.php?GSG_URL=$1

to the .htaccess file.

And that gives me:

http://lodges.goodsafariguide.com/lodgedetails/index/GSG_URL/africanhorseback/

and

http://lodges.goodsafariguide.com/lodgedetails/index/africanhorseback/

I tried:


RewriteRule (.*)/ index.php?GSG_URL=$1
RewriteRule (.*) index.php?GSG_URL=$1

To get to:

http://lodges.goodsafariguide.com/lodgedetails/africanhorseback/

But it didn’t like that, and didn’t show any data.

Is there a tweak in the syntax, or does it always need to reference the .php page?

A short name that can take the place of an ID is typically called a “slug”. And htaccess can certainly rewrite slug URLs to your PHP script, but your script will have to know what to do with a slug.

So if you already have this…

RewriteRule index/LodgeID/(.*)/ index.php?LodgeID=$1

Then you could possibly do something like this…

RewriteRule index/LodgeName/(.*)/ index.php?LodgeName=$1

Then your PHP script will have to check for either LodgeID or LodgeName and look up the record as appropriate.

Now with all that being said, I’d recommend a couple changes to the rewrites. I’d suggest that you add start and end anchors.

RewriteRule [COLOR=“#FF0000”][1]index/LodgeID/(.)/$ index.php?LodgeID=$1
RewriteRule [COLOR=“#FF0000”][2]index/LodgeName/(.
)/$ index.php?LodgeName=$1

The reason is because, without the anchors, then even this URL would match…

gobbledygook/index/LodgeID/288/gobbledygook

And next, I think we can simplify a couple rewrites…

RewriteRule index/LodgeID/(.)/ index.php?LodgeID=$1
RewriteRule index/LodgeID/(.
) index.php?LodgeID=$1

The only difference I see between these two is the trailing slash. If the goal is to make the trailing slash optional, then you can do that with the ? regex metacharacter.

RewriteRule ^index/LodgeID/(.*)/?$ index.php?LodgeID=$1


  1. /COLOR ↩︎

  2. /COLOR ↩︎

Thanks Jeff.

I’ve removed the ones I was using as I went, and now just have the one rule:

RewriteRule ^index/(.*)/ index.php?GSG_URL=$1

So that gets me from:

/lodgedetails/index.php?GSG_URL=africanhorsebacksafaris

to:

/lodgedetails/index/africanhorsebacksafaris/

Is it possible to get to lose the /index/ part and get to:

/lodgedetails/africanhorsebacksafaris/

I tried:

RewriteRule ^index/(.*)/[color=red]?$[/color] index.php?GSG_URL=$1

but it didn’t like it - didn’t pull through any data.

Certainly. To lose the /index/ part from the URL, just lose the /index/ part from the rewrite.

RewriteRule [1]index//COLOR/ index.php?GSG_URL=$1

  • becomes -
    RewriteRule ^(.*)/ index.php?GSG_URL=$1

But with one exception. It used to be that the pattern had to match “LodgeID”, but now you’re matching anything. Even “index.php” will match and try to rewrite to itself. To prevent that, we’ll need to add a condition.

[FONT=Courier New][COLOR="#FF0000"]# If the request is not for a real file...
RewriteCond %{REQUEST_FILENAME} !-f[/COLOR]

# ...then rewrite
RewriteRule ^(.*)/ index.php?GSG_URL=$1[/FONT]

  1. COLOR=“#FF0000↩︎

Thanks Jeff.

Its all working as I wanted it to now. I must have had a typo before when i tried it without the index part and its working now. (I even tried to replicate it not working by taking out the Condition line and the ^, but it still worked)

Thanks again - this is something that has been on my mind to look at for ages so good to get it sorted.