...

View Full Version : Dynamic to Static URL in PHP



xeirus
03-22-2010, 09:29 PM
Hi!
I'm trying to convert my site's dynamic URLs for the past 2 weeks but to no avail, I've tried several different ways but it doesn't seem to work.

My main requirement is that I want my dynamic URLs, for example, ...
http://www.domain.com/item_details.php?language=en&currency=USD&iid=600

To be shown as ...
http://www.domain.com/item_details-language_en-currency_USD-iid_600.html

Instead of using slashes ( / ) and being shown as ... (which is the problem in all the examples I found through Google) ...
http://www.domain.com/item_details/language_en/currency_USD/iid_600.html

If the slashes are used then all my images and included files stop working. So it is better for me to use underscores ( _ ) or hyphens ( - )

I can make the underscores/hyphens based URL work with the following .htaccess :

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

rewriterule ^item_details-([^-]+)-([^&]+)\.html$ /item_details.php?language=$1&currency=$2&iid=$3 [L]

</IfModule>

But the final problem which I'm still facing is that I don't know how to receive the values/variables for "language", "currency" and "iid" to use them on the item_details.php page.

Can someone please help?

Thank you very much!

- Xeirus

MattF
03-22-2010, 09:54 PM
Is there a specific reason for you wanting to break the URI's from their natural format?

xeirus
03-22-2010, 09:57 PM
MattF,
I am doing this for SEO reason.

- Xeirus

MattF
03-22-2010, 10:04 PM
MattF,
I am doing this for SEO reason.

- Xeirus

Is there actually any factual proof that doing so makes the blindest bit of difference though, with regards to S.E.O? Take this forum, for example. By the current supposed S.E.O standards, the dynamic URI's which are used on here should penalise this forum quite notably. Search engine statistics and S.E.R.P's will offer quite a different viewpoint though.

Btw, I'm not trying to digress from the point of your post here, but merely trying to suggest that concentrating more on your sites content and popularity could be far more useful than breaking a standard system, just to adhere to something which I've, (personally), never seen backed up by any proven, factual proof.

xeirus
03-22-2010, 10:11 PM
MattF,
I understand what you're saying. I have tried to explain the same to my bosses but they won't believe me and insist on getting this done.

Fou-Lu
03-22-2010, 10:16 PM
The variables will be accessed the same was as before; the only thing the mod_rewrite will do is alter the appearance of the url itself. Since you're mapping it back to the proper variables here: '/item_details.php?language=$1&currency=$2&iid=$3', the language, currency and iid will all still exist within the $_GET['language'], $_GET['currency'], and $_GET['iid']. Unless I'm completely misunderstanding something here.....

xeirus
03-22-2010, 10:42 PM
Fou-Lu,
Thank you for your reply.

I'm trying to get variables back as :


if (!isset($_GET['language'])) {
$language = 'en';
}
elseif (isset($_GET['language'])) {
$language = $_GET['language'];
}
if (!isset($_GET['currency'])) {
$currency = 'USD';
}
elseif (isset($_GET['currency'])) {
$currency = $_GET['currency'];
}

$iid = $_GET['iid'];

But it doesn't seem to work....

Can you please help?

Thank you.

- Xeirus.

Len Whistler
03-22-2010, 10:44 PM
Is there actually any factual proof that doing so makes the blindest bit of difference though, with regards to S.E.O? Take this forum, for example. By the current supposed S.E.O standards, the dynamic URI's which are used on here should penalise this forum quite notably. Search engine statistics and S.E.R.P's will offer quite a different viewpoint though.

Btw, I'm not trying to digress from the point of your post here, but merely trying to suggest that concentrating more on your sites content and popularity could be far more useful than breaking a standard system, just to adhere to something which I've, (personally), never seen backed up by any proven, factual proof.

Good point. Google "Dynamic to Static URL in PHP", which is the title of this thread and this thread already comes up Number 6 on the first page after one hour.. With results like that a URL rewrite to improve SEO is totally useless.

http://www.google.com/#hl=en&source=hp&q=Dynamic+to+Static+URL+in+PHP&btnG=Google+Search&aq=f&aqi=&aql=&oq=&gs_rfai=&fp=ae8f9588018abe0f




----

xeirus
03-22-2010, 10:59 PM
Len Whistler,
You're right and so is MattF but like I mentioned, I wish, my bosses would understand what you just said!

- Xeirus.

MattF
03-22-2010, 11:12 PM
Have you tried just dumping the $_GET array to output to see what it contains?

xeirus
03-22-2010, 11:46 PM
MattF,
Just tried as you suggested but still keeps throwing out this error:
Error retrieving Item Content from database!
Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

- Xeirus

MattF
03-22-2010, 11:54 PM
I think the penny has just dropped, (and hopefully someone will correct me if I'm wrong). If you're using the new style links in your pages, there won't be any $_GET vars because your webserver is using a 404 handler for the request, hence the info you require will be in the $_SERVER array. You'll need to use the likes of parse_url and such to strip the info you require from that. I believe $_SERVER['REQUEST_URI'] will contain the info you need to parse.

Btw, you really ought to be checking your vars before running a DB query that will obviously fail. It sounds like you're running the query regardless of whether any required input is there or not.

xeirus
03-23-2010, 12:02 AM
MattF,
Yes, I just tried that and here's what I get from $_SERVER['QUERY_STRING'] ....
language=language¤cy=en-currency_USD-iid_424&iid=
Completely confusing for my brain's little PHP knowledge area! LOL ... and look at that unknown character, don't know where that came from!

And here's what I get from $_SERVER['REQUEST_URI'] ...
/item_details-language_en-currency_USD-iid_424.html

This one looks much better!
Please tell me where do I go from here as I don't know how to parse, break or use this info to get my variables back?

Thank you!

- Xeirus

MattF
03-23-2010, 12:03 AM
Good point. Google "Dynamic to Static URL in PHP", which is the title of this thread and this thread already comes up Number 6 on the first page after one hour.. With results like that a URL rewrite to improve SEO is totally useless.

I'm personally of the line of thought that it serves no purpose other than prettifying the links in the address bar. In all this time, no matter how much people bang on about it being important for S.E.O, I've never seen anything to suggest that it has any effect, and can't say that any of the supposed experts have ever proven that it does either. At the end of the day, a URI is a URI, regardless of appearance. I personally just class it as merely a money making scheme, (for those so-called experts), or just the current fad for everyone else, but nothing more.


p.s: xeirus, we're not questioning your ongoing question/requirement here, but rather just having a sub conversation within your thread. :) Hope you don't mind?

xeirus
03-23-2010, 12:08 AM
MattF,
Not at all. In fact, it is useful for me as I will try to show this conversation between you and Len Whistler, to my bosses as a proof to make them believe that I (just a kid when it comes to PHP) am not being lazy or anything, but you experienced coders say the same!

- Xeirus.

MattF
03-23-2010, 12:10 AM
Yes, I just tried that and here's what I get from $_SERVER['QUERY_STRING'] ....
language=language¤cy=en-currency_USD-iid_424&iid=
Completely confusing for my brain's little PHP knowledge area! LOL ... and look at that unknown character, don't know where that came from!

And here's what I get from $_SERVER['REQUEST_URI'] ...
/item_details-language_en-currency_USD-iid_424.html


That's definitely looking better. Btw, I'm no expert on this rewrite stuff, so I'll help if I can but can't guarantee anything.




This one looks much better!
Please tell me where do I go from here as I don't know how to parse, break or use this info to get my variables back?


The first thing to do is parse $_SERVER['QUERY_STRING']. These functions are the ones you want to be looking at:

http://uk.php.net/manual/en/function.parse-url.php
http://uk.php.net/manual/en/function.parse-str.php
http://uk.php.net/manual/en/function.pathinfo.php

A combination of those should give you what you need. If not, it'll be a case of dropping back to a regex solution methinks. Hopefully someone with a bit more experience in this area may jump in though.

xeirus
03-23-2010, 12:53 PM
MattF,
Thank you very much indeed for your help!
For others like me who would like to know how to get Dynamic to Static URLs in PHP with underscores ( _ ) and hyphens ( - ) instead of slashes ( / ) ... here's how our final .htaccess looks:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

rewriterule ^item_details-([^-]+)-([^&]+)\.html$ /item_details.php?language=$1&currency=$2&iid=$3&keywords=$4 [L]

</IfModule>

Note that I've added description=$4 as it does 2 useful things
a) keywords=$4 will be holding all my keywords for that particular item that my bosses want the search engines and normal users to see.
b) iid=$3 was not showing up in the variables because if it is the last variable then the last variable gets thrown out with a ".html" at the end of it but with the "keywords" variable added behind it, one more hyphen ( - ) gets added which throws out the iid=$3 without the ".html" at its end

And here's how our final PHP code (to get the variables) looks like:



$Server_Query_String1 = $_SERVER['QUERY_STRING'];
echo $Server_Query_String1;
print '<br>';
$Server_Request_URI1 = $_SERVER['REQUEST_URI'];
echo $Server_Request_URI1;

$data = explode("-",$_SERVER['REQUEST_URI']);
$language = $data[2];
$currency = $data[4];
$iid = $data[6];
$keywords = $data[8];
print '<br>';
echo $language;
print '<br>';
echo $currency;
print '<br>';
echo $iid;
print '<br>';
echo $keywords;


Thanks once again!

- Xeirus.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum