Jump to content

Using Mod_rewrite with a database query


Merdok

Recommended Posts

Hi guys,

 

So my site currently displays pages like this:

 

http://www.digitalfusiondev.me/modules/blog/article.php?cat=4&article=53

 

I want to use mod_rewrite to display them like this

 

http://www.digitalfusiondev.me/software/five-things-you-didnt-know-you-couldnt-live-without

 

The problem I have there is that my site uses ID's for the queries, I can change it to use names but that will cause issues for the existing incoming links so is there a way I can get mod_rewrite to retrieve the category name and article title dynamically?

 

Link to comment
Share on other sites

Yes and no. It is for this sort of solution that mod_rewrite is used, but it doesn't have anything to do with your database directly. All mod_rewrite does is takes an address that the user asked for and returns a different address, thus allowing you to create 'aliases' for the URLs. It uses Regular Expressions to allow pattern matches for such aliases making it a powerful system. The first thing you need to do is change all hrefs on your site to the desired pattern of...

 

http://www.digitalfusiondev.me/software/five-things-you-didnt-know-you-couldnt-live-without

 

You would then use mod_rewrite in a manner like so...

 

RewriteRule ^([a-zA-Z-]+)/([a-zA-Z-]+)/?$ /modules/blog/article.php?cat=$1&article=$2

 

This will take you to the correct page and will set $_GET['cat'] = 'software' and $_GET['article'] = 'five-things-you-didnt-know-you-couldnt-live-without'. So you will need to adjust your system to look up these string values in the database as 'permalinks' rather than the numerical ids. The only way around this final stage is to incorporate the numbers into the URL in some manner for example....

 

http://www.digitalfusiondev.me/4-software/53-five-things-you-didnt-know-you-couldnt-live-without

 

You would then use mod_rewrite in a manner like so...

 

RewriteRule ^([0-9]+)-[a-zA-Z-]+/([0-9]+)-[a-zA-Z-]+/?$ /modules/blog/article.php?cat=$1&article=$2

 

 

Link to comment
Share on other sites

Right I've rewritten my scripts to accept text input (didn't take as long as I thought  :D ), I've managed to get the mod_rewrite working for categories so if you type in /design it will take you to the design category but I can't get it to find the article.

 

In the database the article title looks like this "Top 5 things that webdesigners use in photoshop"

 

The page itself sees it as Top+5+things+that+webdesigners+use+in+photoshop as all my article links are urlencoded

 

How can I get mod_rewrite to see "top-5-things-that-webdesigners-use-in-photoshop" as "Top+5+things+that+webdesigners+use+in+photoshop"?

 

I know about the [NC] tag and that definately works for the categories but it seems that maybe the titles are a little more complicated that I realised.

 

A few of the articles also have "-"'s and ":"'s in them, will this cause issues for mod_rewrite? I got around it in php by just encoding all the url's but I'm not sure how to do it in mod_rewrite, if you can at all.

Link to comment
Share on other sites

Put simply you can't, the recommended solution is to add a permalink field to your database which is the title but made url friendly, there are lots of solutions out there but generally speaking this will involve replacing spaces or any non-alphanumeric characters with a dash and storing that in the database at the same time as the article.

Link to comment
Share on other sites

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.