tags:

views:

97

answers:

3

Hello there everyone.

I currently have an entire website running on PHP & GET variables.

My links look like this at present

 http://www.example.co.uk/brochure.php?cat_path=24

And occasionally it has a second variable in the url...

I actually would like my URLs to look like this: http://www.example.co.uk/Concrete_Fence_Posts

So my questions is simply, how do I go about rewriting urls without breaking my GET variables.

Thanks.

+2  A: 

Assuming your parameters are always named cat_path and product_id (if it exists) you can do something like this:

RewriteRule ^([^/]+)/([\d+])$         $1.php?cat_path=$2
RewriteRule ^([^/]+)/([\d+])/([\d+])$ $1.php?cat_path=$2&product_id=$3

Your URLs would then be in one of these formats:

pagename/cat_path
pagename/cat_path/product_id

For example:

http://www.bentinckfencing.co.uk/brochure/24
http://www.bentinckfencing.co.uk/product/35/54

Edit: I see you want to use product names in the URL. In that case, your PHP scripts will need to be able to take a name as a parameter and look up the ID. You should continue to accept the ID directly so as to not break existing links. Then your rewrite rule would look like this:

RewriteRule ^([^.]+)$         brochure.php?name=$1

And http://www.bentinckfencing.co.uk/Concrete_Fence_Posts would rewrite to http://www.bentinckfencing.co.uk/brochure.php?name=Concrete_Fence_Posts.

casablanca
A: 

For better search search engine optimisation have URLs like stackoverflow -

http://www.bentinckfencing.co.uk/brochure/24/concrete-fence-posts

There is something like search engines do not store whatever is after the ? in your URLs. This ending concrete-fence-posts also helps users to identify the page in the browser address bar. But, you cannot allow certain special characters if the ? is not used in the URL. Characters like /, % will break the URL and \, #, + will not appear.

Updates

Do you want to move completely from ID passing to name passing? I am not sure about how good that will be, but I have heard that a few well known sites have URLs like that. But, you may have to do many changes depending on your project (how many modules you have). The stackoverflow style URL will, however, retain the ID based item fetching.

Further Updates
You can try this rewrite rule to have both ID and name in the URL (stackoverflow style):- RewriteRule ^([a-z0-9-_]+)/([0-9]+)/([a-z0-9-_]+)/?$ $1.php?cat_path=$1&cat_name=$2

I have not tested it though.

Another important thing, I see that your code enters brochure.php directly and there is no common file (common controller) which is executed in case of all the modules. This approach can have many drawbacks, particularly if your's is a medium to large application. It is always better to execute a common index.php file and include the needed brochure.php file there so that you can perform all those common logics (needed in all your modules) in this index.php file. I have a common index.php file and the following rewrite rules do the same:-

############To remove display of "index.php" from URL
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)$ index.php [L,QSA]
#################################################end of hiding index.php

RewriteRule ^([a-z0-9\-\_]+)/([0-9]+)/([a-zA-Z0-9\-\_]+)/?$ index.php?a=$1&b=$2&c=$3

Note:- The first block of rewrite rules hides the index.php from the URL and a visitor does not know that my application runs in PHP.

Finally I have URLs like this:-

http://www.bentinckfencing.co.uk/brochure/24/concrete-fence-posts

Hope this helps,
Sandeepan

sandeepan
This sounds like a perfect solution, as I'd like to stick with fetching results via ID, not Name. I can figure out how to get the name into the url (with another GET variable), but I'm not sure how I'd actually go about the rewrite. What code do I need for this?
shane
@shane, check my Further Updates section
sandeepan
A: 

I've answered a similar question recently:
http://stackoverflow.com/questions/3439430/how-can-i-use-in-htaccess/3439495#3439495
complete solution to move on new urls

Col. Shrapnel