I would like to make my urls more seo friendly and for example change this:
to something nice like this:
http://www.chillisource.co.uk/product/Daves_Gourmet/Daves_Insanity_Sauce
What is the best way of going about doing this? I've had a look at doing this with the htaccess file but this seems very complicated.
Thanks in advance
It's called a mod_rewrite, here is a tutorial:
http://www.workingwith.me.uk/articles/scripting/mod_rewrite
try some thing like this
then use
$_GET
to get the parameter values...What about using the
PATH_INFO
environment variable?Will output
The transition from using
$_GET
to usingPATH_INFO
environment is a good programming exercise. I think you cannot just do the task with configuration.Ben Paton, there is in fact a very easy way out. CMSes like Wordpress tend to use it instead of messing around with regular expressions.
The
.htaccess
sideFirst of, you use an .htacess with the content below:
Let me explain what it does (line by line):
RewriteBase /subdir/
)The above is just a quick explanation. You don't really need it to use this.
What we did, is that we told Apache that all requests that would end up as 404s to pass them to the index.php file, where we can process the request manually.
The PHP side
On the PHP side, inside
index.php
, you simply have to parse the original URL. This URL is passed in the$_SERVER
variable as$_SERVER['REDIRECT_URL']
.The best part, if there was no redirection, this variable is not set!
So, our code would end up like:
Easy Integration
I nearly forgot to mention this, but, thanks to the way it works, you can even end up not changing your existing code at all!
Less talk, more examples. Let's say your code looked like:
Which worked with urls like:
You can make it work with SEO URLs as well as keep it with your old system at the same time. Firstly, make sure the
.htaccess
contains the code I wrote in the one above.Next, add the following code at the very start of your file:
What are we doing here? Before going on two your own code, we are basically finding IDs and information from the old URL and feeding it to PHP's $_GET variable. We are essentially fooling your code to think the request had those variables!
The only remaining hurdle to find all those pesky
<a/>
tags and replace theirhref
accordingly, but that's a different story. :)I'll have to add: in your original url, there's a 'prod' key, which seems to consist of an ID. Make sure that, when switching to rewritten urls, you no longer solely depend upon a unique id, because that won't be visible in the url. Now, you can use the ID to make a distinction between 2 products with the same name, but in case of rewriting urls and no longer requiring ID in the url, you need to make sure 1 product name can not be used multiple times for different products. Likewise, I see the 'cat'-key not being present in the desired output url, same applies as described above.
Disregarding the above-described "problems", the rewrite should roughtly look like:
The q & prodName will receive the underscored value, rather than %20, so also that will require some patching. As you can see, I didn't touch the id & category, it'll be up to you to figure out how to handle that.