developerdave Posted March 26, 2010 Share Posted March 26, 2010 Morning guys, I'm gonna straight away put my hands in the air and say I'm bit of a mod_rewrite newbie with only the basics and I'm looking for a head start on how to rewrite this ugly url to a friendly url So for example http://mywebsite.co.uk/item.php?id=100 rewritten to http://mywebsite.co.uk/item/projector/ I know it can be done so I won't ask that, but from what I've seen (all the $1, $2 business) it doesn't make a whole amount of sense to me, could someone shed some light on this for me as its a skill I'd love to learn. Quote Link to comment https://forums.phpfreaks.com/topic/196584-mod_rewrite-for-seo/ Share on other sites More sharing options...
GalaxyTramp Posted March 26, 2010 Share Posted March 26, 2010 Hi Dave RewriteEngine On RewriteRule ^projector/$ /item.php?id=100 [L] Will output in the browser address bar as: http://mywebsite.co.uk/projector/ So basically if a page starts with ^projector rewrite it to /item etc. The $ sign denotes the end of the string to be matched and [L] tells Apache to stop here if rewrite conditions are met. So links on your site to http://mywebsite.co.uk/projector/ would be rewritten on the server to http://mywebsite.co.uk/item.php?id=100 You can find several online tools to achieve this such as http://www.generateit.net/mod-rewrite/ No point in reinventing the wheel so google mod rewrite and you will find plenty of tutorials explaining the intracacies of this method. GT Quote Link to comment https://forums.phpfreaks.com/topic/196584-mod_rewrite-for-seo/#findComment-1032177 Share on other sites More sharing options...
AdRock Posted March 26, 2010 Share Posted March 26, 2010 RewriteRule ^/?item/([a-zA-Z)$ item.php?id=$1 [NC,L] Quote Link to comment https://forums.phpfreaks.com/topic/196584-mod_rewrite-for-seo/#findComment-1032179 Share on other sites More sharing options...
developerdave Posted March 26, 2010 Author Share Posted March 26, 2010 Thanks guys, I did do some Google'ing but as I say 99% of the examples are pretty confusing considering I only know the basic (.*) method lol. I'll test these out in a bit, I have to install a SSL cert now lol T.T Quote Link to comment https://forums.phpfreaks.com/topic/196584-mod_rewrite-for-seo/#findComment-1032186 Share on other sites More sharing options...
developerdave Posted March 26, 2010 Author Share Posted March 26, 2010 Thanks guys, I did some thinking and it turns out I could have done it a lot simpler and done it a much easier way round. I've decided to use friendly urls and rewrite them to request the ugly one in the background (no redirect) So the url will read www.example.com/projector/100 And the mod_rewrite something along the lines of /(.*)/(.*)/ item.php?id=$2 Which is much simpler lol, thanks for your help guys Quote Link to comment https://forums.phpfreaks.com/topic/196584-mod_rewrite-for-seo/#findComment-1032301 Share on other sites More sharing options...
developerdave Posted March 29, 2010 Author Share Posted March 29, 2010 Hey guys, apparently that didn't want to work and I'm stumped with all this mod_rewrite malarky. I've tried using AdRock's example, modified slightly to RewriteRule ^/?item/([a-zA-Z)/([a-zA-Z)$ item.php?id=$2 [NC,L] which I was convinced would have worked lol. Can anyone help please? Quote Link to comment https://forums.phpfreaks.com/topic/196584-mod_rewrite-for-seo/#findComment-1033393 Share on other sites More sharing options...
cags Posted March 29, 2010 Share Posted March 29, 2010 What URL are you trying to accept as an input and what URL should Apache actually be serving up? I'm guessing you need a forward slash before item.php you don't need the /? at the start of the first pattern you possibly need something like that at the end of that pattern, it depends exactly what URLs you are trying to match. Quote Link to comment https://forums.phpfreaks.com/topic/196584-mod_rewrite-for-seo/#findComment-1033407 Share on other sites More sharing options...
developerdave Posted March 29, 2010 Author Share Posted March 29, 2010 I've been trying something like this ^/(.*)/(.*)$ /item.php?id=$1 But to no avail I'm trying to rewrite something like this www.example.com/macbook-pro/5 to serve item.php?id=5 Quote Link to comment https://forums.phpfreaks.com/topic/196584-mod_rewrite-for-seo/#findComment-1033419 Share on other sites More sharing options...
cags Posted March 29, 2010 Share Posted March 29, 2010 I'm not sure why you were convinced your previous pattern would work then since the first 'directory' includes a dash which you didn't allow and the second is a number which you also didn't allow. You also had no quantifier meaning you were only matching a single character. You pattern wants to be something along the lines of the following... RewriteRule ^([a-zA-Z-]+)/([0-9]+)/?$ /item.php?id=$2 [NC,L] Quote Link to comment https://forums.phpfreaks.com/topic/196584-mod_rewrite-for-seo/#findComment-1033431 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.