Jump to content

mod_rewrite for seo


developerdave

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/196584-mod_rewrite-for-seo/
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/196584-mod_rewrite-for-seo/#findComment-1032177
Share on other sites

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  :D

Link to comment
https://forums.phpfreaks.com/topic/196584-mod_rewrite-for-seo/#findComment-1032301
Share on other sites

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?

Link to comment
https://forums.phpfreaks.com/topic/196584-mod_rewrite-for-seo/#findComment-1033393
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/196584-mod_rewrite-for-seo/#findComment-1033407
Share on other sites

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]

Link to comment
https://forums.phpfreaks.com/topic/196584-mod_rewrite-for-seo/#findComment-1033431
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.