domain.com/key1-value1-key2-value2-keywhatever-value.htm

Discuss practical ways rearrange URLs using mod_rewrite.

domain.com/key1-value1-key2-value2-keywhatever-value.htm

Postby juglesh » Sat Dec 09, 2006 6:50 pm

Hi there,
I thought I once found a way to have as many key-value pairs as i wanted, with the last one being value.htm. So that my url looks like a static .htm page:
Code: Select all
domain.com/key1-value1-key2-value2-keywhatever-value.htm
should be turned into:
Code: Select all
domain.com/index.php?key1=value1&key2=value2&keywhatever=vaule
I would want a rewrite rule that i could name as many key-value pairs as I want, and I want to be able to name the key.

So the rewrites to
Code: Select all
index.php?key1=$1
doesnt work, because it should parse that key1 variable name by the url.

I guess another way would be to rewrite the whole 'file name' into a var, and then parse it myself with php to get the key/values out of it.

thanks,
juglesh
juglesh
 
Posts: 5
Joined: Mon Nov 21, 2005 11:38 am

Postby richardk » Sun Dec 10, 2006 7:39 am

I guess another way would be to rewrite the whole 'file name' into a var, and then parse it myself with php to get the key/values out of it.

That's the best thing to do.

Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^.+\.htm$ /index.php?mr [L]


Code: Select all
if(isset($_GET['mr']))
{
  $uri = explode('?', getenv('REQUEST_URI'), 2);
  $parts = explode('-', substr($uri[0], 1, strlen($uri[0]) - 5));
  if((count($parts) ^ 2) == 1)
  {
    $parts[] = null;
  }
  for($i = 0; $i < count($parts); $i = $i + 2)
  {
    $_GET[$parts[$i]] = $parts[$i + 1];
  }
  unset($_GET['mr'], $uri, $parts, $i);
}
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby juglesh » Sun Dec 10, 2006 10:41 am

Works perfect, thanks!
2??s

Why is that "the best thing to do. " ? As opposed to having m_r do it ?

What is "RewriteCond %{SCRIPT_FILENAME} !-f " doing?
juglesh
 
Posts: 5
Joined: Mon Nov 21, 2005 11:38 am

Postby richardk » Sun Dec 10, 2006 11:02 am

I think using PHP is better because the you only need one RewriteRule.

Code: Select all
RewriteCond %{SCRIPT_FILENAME} !-f

Means don't rewrite is the file requested is real. The SCRIPT_FILENAME holds the full physical server path to the file requested, ! means not, and -f checks it it is a file or not.

http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html#rewritecond
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby juglesh » Sun Dec 10, 2006 1:09 pm

how come this is empty? (before the unset() at the end of your script)
Code: Select all
echo '$get-mr='.$_GET['mr'].'<BR>';
I thought your rewrite rule was sticking the 'file name' in to $_GET['mr'], but on further looking into it, i guess its not. Its just being set, so you dont alter your regular $_GET if the rewrite condition is not true. So, I guess I answered my own question, correct?
juglesh
 
Posts: 5
Joined: Mon Nov 21, 2005 11:38 am

Postby richardk » Sun Dec 10, 2006 3:01 pm

Yeah, $_GET['mr'] never has a value, it's so it doesn't run the if and set the GET variables when it's not supposed to. The path comes from getenv('REQUEST_URI') (the same as $_SERVER['REQUEST_URI']).
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am


Return to Friendly URLs with Mod_Rewrite

Who is online

Users browsing this forum: No registered users and 2 guests

cron