Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/196171-dynamic-to-static-url-in-php/
Share on other sites

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'); ?>" ...

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.

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

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&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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.