0

How can I turn example 1 into example 2 using PHP

Example 1

http://www.example.com/categories/fruit/apple/green

Example 2

http://www.example.com/categories/index.php?cat=fruit&sub1=apple&sub2=green
6
  • Not really the job for PHP, look into mod_rewrite or similar (depending on what http server you're running on). Commented Dec 24, 2010 at 8:10
  • @Bhanu, @Reese Moore, an example would be helpful Commented Dec 24, 2010 at 8:11
  • @HeLp what do you mean by 'turn into'? If you want to cause a redirect, yes you need to use htaccess if you need to rewrite data that you are turning into links then yes, you could do it in PHP with a regex, please clarify exactly what you want to do. Commented Dec 24, 2010 at 8:16
  • @tobyodavies, I really dont no what you mean but an example of both would help me clear up things. Commented Dec 24, 2010 at 8:19
  • What do you want to do with these urls? serve URL1 as if it were URL2, display them to people, bake them a cake? We need more details to answer the question. Commented Dec 24, 2010 at 8:21

2 Answers 2

1

As has been suggested, this is something to be done through HTACCESS and mod_rewrite rules. The best trick for things like this would be to have a go, share what you have managed to come up with (and the results or bugs) and people will then help you find a complete solution.

That being said, I would suggest something like the following, in a file called ".htaccess" in your webroot.

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_URI} !-f # Means if the requested address is not a file
RewriteCond %{REQUEST_URI} !-d # Means if the requested address is not a dir
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ index.php?cat=$1&sub1=$2&sub2=$3

I have not tested the above code, but it would be where I would start, and maybe mix with some independent research as required...

Some links:

Sign up to request clarification or add additional context in comments.

Comments

0

That's just simple string manipulation - but note that changing the request URI within* your script won't automatically populate the relevant $_GET variables.

To change the string:

  <?php
  $input_url='http://www.example.com/categories/fruit/apple/green';
  $parts=parse_url($input_url);
  $embeds=explode('/',$parts['path']);
  $new_path=array_shift(embeds) . '/index.php'; // store 'categories' for later
  $count=1;
  $join=strlen($parts['qry']) ? '&' : '?';
  foreach ($embeds as $val) {
      $parts['query'].=$join . 'ub' . $count . '=' . urlencode($val);
      $join='&';
  }
  $out_url=$parts['scheme'] . '://' 
     . ($parts['username'] . $parts['password']) ? 
             $parts['username'] . ':' $parts['password'] . '@' : ''
     . $parts['host'] . '/' . $new_path
     . $parts['query']
     . '#' . $parts['fragment'];
  print $out_url;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.