Parsing URLs using PHP

1.3k Views Asked by At

I have posted a similar question here. However, this was more about getting advice on what to do. Now that I know what to do, I am looking for a little help on how to do it!

Basically I have a website that is pretty much 100% dynamic. All the website links are generated using PHP and all the pages are made up of php includes/code. I am trying to improve the SEO of the site by improving the URLs (as stated in the other question) and I am struggling a little.

I am using mod_rewrite of rewriting the nice urls to the ugly urls on the server. So what I need is to now convert the ugly urls (which are generated from the php code in the pages) to the nicer urls.

Here are the URLs I need to parse (these are in the other question aswell):

/index.php?m=ModuleType
/index.php?m=ModuleType&categoryID=id
/index.php?m=ModuleType&categoryID=id&productID=id
/index.php?page=PageType
/index.php?page=PageType&detail=yes

Here is what I want the above URLs to be parsed to:

/ModuleType
/ModuleType/CategoryName
/ModuleType/CategoryName/ProductName
/PageType
/PageType/Detail

There is an example on the other question posted by Gumbo however I felt it was a bit messy and unclear on exactly what it was doing.

Could someone help me solve this problem?

Thanks in advance.

4

There are 4 best solutions below

5
On BEST ANSWER

I think I see what you're after... You've done all the URL rewriting, but all the links between your pages are using the old URL syntax.

The only way I can see around this is to do some kind of regex search and replace on the links so they use the new syntax. This will be a bit more complicated if all the links are dynamically generated, but hopefully there won't be too much of this to do.

Without seeing how your links are generated at the moment, it's difficult to say how to change the code. I imagine it works something like this though:

<?php echo "<a href='/index.php?m=$ModuleType&categoryID=$id'>"; ?>

So you'd change this to:

<?php echo "<a href='$ModuleType/$id'>"; ?>

Sorry if I've made errors in the syntax, just off the top of my head...

1
On

Unless I misunderstood your question, you don't parse the "ugly" URLs, your PHP script is called with them, so you $_GET[] your parameters (m, categoryID, productID) and you combine them to make your nice URLs, which shouldn't be too hard (just a bit of logic to see if one parameter is there and concatenate the strings).

2
On

You will need a front controller, which will dispatch the URL to the correct page.

  1. Apache will rewrite the URL using rules in .htaccess, so that anything written will be redirected to index.php?q=. For example, typing http://example.com/i/am/here will result in a call to index.php?q=/i/am/here
  2. Index.php will parse the path from $_GET["q"] and decide what to do. For example, it may include a page, or go to the database, look the path up, get the appropriate content and print it out

If you want a working example of a .htaccess which will do exactly that (redirect to index.php with ?q=path) take a look at how drupal does it:

http://cvs.drupal.org/viewvc.py/drupal/drupal/.htaccess?revision=1.104

3
On

As Palantir wrote this is done using mod_rewrite and .htaccess. To get the correct rewrite conditions into your .htaccess you might want to take a look at a Mod Rewrite Generator (e.g. http://www.generateit.net/mod-rewrite/). Makes it a lot easier.