Forum Moderators: phranque

Message Too Old, No Replies

Please help with mod rewrite

         

Hrist0

11:43 pm on Mar 16, 2010 (gmt 0)

10+ Year Member



Hello to everybody in this forum,

First of all I'd like to appologize for asking you this, but really give up searching in Google for solution. May be its because my English is not so good and I don't know how to ask Google properly, may be its because I am a begninner with mod_rewrite. Result is that for last couple of days I read a lot of and test a lot of functions, but could not receive below efect. Could you please help me with this:

1. I've got URL from type: http://www.example.com/product_info.php?prod_id=767&id=36&language=Bg

This page is about product with name in data base for example - "Product with specification - one"

I want to transform it to: http://www.example.com/products/Product_with_specification_one

2. I want new URL http://www.example.com/products/Product_with_specification_one to be displayed in address bar in browsers

3. Got link from type http://www.example.com/index.php?id=3&language=Bg

Its about category of products with name from DB for example "Category with name one". Want to convert name to http://www.example.com/category/category_with_name_one

4. Want new link http://www.example.com/category/category_with_name_one to be displayed in address bar in browser

5. Last question, that not sure should ask - is it going to reflect at all this rewrite rules last parameter from database - BG and ENG. What if client type in a browser - http://www.example.com/category/category_with_name_one - how will it knew to redirect to Bulgarian or English version? May be new link should be http://www.example.com/category/category_with_name_one_bg or http://www.example.com/category/category_with_name_one_en

I'd like to thank to everybody in advance who help me to solve this. Reason to ask in this forum is because a lof of replies in Google to my questions redirect here, so hope that here is the right place to ask and to get promp reply. Thank you!

jdMorgan

6:07 pm on Mar 17, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In your second-to-last paragraph, you identified the most important question: "How will it know?" And the answer is that "it cannot know."

The information to associate "Product_with_specification_one" to "prod_id=767" and to determine Bulgarian/English language selection is not available in the requested URL, and so is not available to mod_rewrite.

The only information that mod_rewrite will have available to it is in the "/products" part of the requested URL-path. This dictates the solution, which is:

1) Change the links on your HTML pages from "http://www.example.com/product_info.php?prod_id=767&id=36&language=Bg" to "http://www.example.com/products/Product_with_specification_one". This can be done by modifying your script to pull the "friendly" URL from the database instead of getting the "unfriendly URL with the prod_id in the query string" as it does now. Or you could add code after the script gets the unfriendly URL to do another lookup to "translate" that unfriendly URL to the friendly one.

The first method works better if all the data is in one database, while the second method would work better if you decided to put the URL translations into a second database. Generally, it will be easier to maintain your site with all the data kept together in one database, but I do not know anything about your existing script, and so have to present both options.

2) Write a mod_rewrite rule to internally rewrite all requests for URL-paths beginning with "products" to your script (or to a new "wrapper" script, which makes the new-URL-to-product id association and then "includes" your original script).

You will need to pass the "Product_with_specification_one" part of the requested URL-path to this script, or have your script get it from the server variable containing the client-requested URL. In this script use the "Product_with_specification_one" part of the client-requested URL to look up the correct product-id (prod_id=767), and then proceed as before.

3) Detect direct client requests for the old "http://www.example.com/product_info.php?prod_id=767&id=36&language=Bg" URLs (which are now used only as internal script filepaths), and externally redirect them to the new friendly URLs. Again, this will have to be done using both mod_rewrite and your script, because again, a database lookup will be required.

The mod_rewrite code for step 2 is rather trivial. For the "pass friendly product name as parameter to your existing script" approach, it would be something like:

RewriteRule ^products/(.*)$ /index.php?product_name=$1 [L]

So, as you can see, several modifications and additions to your script will be needed. If possible, these should be done within your existing script. However, if you use a commercial script which is updated frequently (to add features and improve security), then the idea of using a "wrapper" script will likely be more attractive. In this way, the commercial script can be left as-is, meaning that you can upgrade it any time you like, while the "wrapper" script that calls it can be used to make the "custom changes" to the input parameters.

However, it is still likely that you'll want to make the changes to publish 'friendly' URLs on your HTML pages inside the main script, because doing it any other place may be too difficult or inefficient. So, these changes should be kept all together in one area of the script, and be well-documented, to make "patching" the upgraded script easier.

Jim

Hrist0

9:36 pm on Mar 17, 2010 (gmt 0)

10+ Year Member



Dear Jim,

First of all - Thank you very much for your time and your reply. I am really impressed of everything you wrote, but after that I realize one think - I am not so good as I tought and have some blanks in my knowidge in php. I will try to find somebody in Bulgaria to explain to me what you mean and hope that soon or later I will solve this. I don't want to be cheeky, but if you have good will /not that you don't have till now :-)/ I can share more info about structure of my website and DB and then if you can write rules will be nice. Anyway - thank you again.

jdMorgan

3:29 am on Mar 18, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You only need the one rule posted above to start with. All the rest of the work goes into your script.

Hopefully, you can find a good "technical translator" to help -- I can't imagine trying to work on server problems with *both* technical and language/translation difficulties at the same time!

Jim

Hrist0

11:54 pm on Apr 4, 2010 (gmt 0)

10+ Year Member



Hello,

Just to close this topic, I'd like to share that I've finished with this links. Result can be seen here <snip>

Thanks for help to everybody.

[edited by: jdMorgan at 2:06 am (utc) on Apr 5, 2010]
[edit reason] Sorry, no URLs. Please see Terms of Service and Charter. [/edit]