xeirus Posted March 22, 2010 Share Posted March 22, 2010 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¤cy=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¤cy=$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 Quote Link to comment https://forums.phpfreaks.com/topic/196171-dynamic-to-static-url-in-php/ Share on other sites More sharing options...
ignace Posted March 22, 2010 Share Posted March 22, 2010 If the slashes are used then all my images and included files stop working. That's because you use relative paths like images/my-image.png which leads to the absolute path http://www.domain.com/item_details/language_en/currency_USD/images/my-image.png You can solve this by using either <base> or writing absolute paths instead of relative: function get_image($image_filename) { return HTTP_BASE_PATH . $image_filename; } use as: <img src="<?php get_image('images/my-image.png'); ?>" ... Quote Link to comment https://forums.phpfreaks.com/topic/196171-dynamic-to-static-url-in-php/#findComment-1030195 Share on other sites More sharing options...
xeirus Posted March 22, 2010 Author Share Posted March 22, 2010 Hi ignace, Thank you for your reply. But that is one of the ways I am trying to ignore as then I will have to go and make such changes throughout the whole site. Is there any other way? Thank you. - Xeirus Quote Link to comment https://forums.phpfreaks.com/topic/196171-dynamic-to-static-url-in-php/#findComment-1030203 Share on other sites More sharing options...
ignace Posted March 22, 2010 Share Posted March 22, 2010 Depends do you have a header file or something? If you have such file then you can use the <base> option otherwise I would take a look at .htaccess to rewrite your image paths from relative to absolute. Quote Link to comment https://forums.phpfreaks.com/topic/196171-dynamic-to-static-url-in-php/#findComment-1030221 Share on other sites More sharing options...
xeirus Posted March 22, 2010 Author Share Posted March 22, 2010 ignace, No I don't have header/footer files, all my files are single files but they do have simple includes like function.php files etc.. Yes, .htaccess could be rewritten, to remap images folder and css files etc. But like I said, I like the look of _ or - instead of slashes and I've seen sites do it, so I need to know how do they do it. Please help. Thank you! - Xeirus. Quote Link to comment https://forums.phpfreaks.com/topic/196171-dynamic-to-static-url-in-php/#findComment-1030227 Share on other sites More sharing options...
ignace Posted March 22, 2010 Share Posted March 22, 2010 Route all your request through your item_details.php file so that: http://www.domain.com/item_details-language_en-currency_USD-iid_600.html Is rewritten to: http://www.domain.com/item_details.php/item_details-language_en-currency_USD-iid_600.html Then add additional lines to the start of your item_details.php so that it reads something similar to: if ('images' === substr($_SERVER['PATH_INFO'], 0, 6)) { $image = 'images' . DIRECTORY_SEPARATOR . basename($_SERVER['PATH_INFO']); if (!file_exists($image)) { header('HTTP/1.0 404 Not Found'); exit(0); } echo file_get_contents($image); exit(0); } Take a look at http://php.net/manual/en/reserved.variables.server.php for more information regarding PATH_INFO Quote Link to comment https://forums.phpfreaks.com/topic/196171-dynamic-to-static-url-in-php/#findComment-1030234 Share on other sites More sharing options...
xeirus Posted March 23, 2010 Author Share Posted March 23, 2010 ignace, 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¤cy=$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. Quote Link to comment https://forums.phpfreaks.com/topic/196171-dynamic-to-static-url-in-php/#findComment-1030445 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.