Merdok Posted March 19, 2010 Share Posted March 19, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/195863-using-mod_rewrite-with-a-database-query/ Share on other sites More sharing options...
cags Posted March 19, 2010 Share Posted March 19, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/195863-using-mod_rewrite-with-a-database-query/#findComment-1028811 Share on other sites More sharing options...
Merdok Posted March 19, 2010 Author Share Posted March 19, 2010 Ah ok, I wanted to avoid rewriting my database queries but I guess that way does make the most sense after all. Well that's my weekend taken care of Thank you for the speedy reply Quote Link to comment https://forums.phpfreaks.com/topic/195863-using-mod_rewrite-with-a-database-query/#findComment-1028818 Share on other sites More sharing options...
Merdok Posted March 20, 2010 Author Share Posted March 20, 2010 Right I've rewritten my scripts to accept text input (didn't take as long as I thought ), 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. Quote Link to comment https://forums.phpfreaks.com/topic/195863-using-mod_rewrite-with-a-database-query/#findComment-1028851 Share on other sites More sharing options...
cags Posted March 20, 2010 Share Posted March 20, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/195863-using-mod_rewrite-with-a-database-query/#findComment-1028858 Share on other sites More sharing options...
Merdok Posted March 20, 2010 Author Share Posted March 20, 2010 oh I see.. ok thank you Quote Link to comment https://forums.phpfreaks.com/topic/195863-using-mod_rewrite-with-a-database-query/#findComment-1028859 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.