views:

178

answers:

4

Hi,

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.

+1  A: 

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

PhiLho
all the links on the web pages right now are parameterized i.e. index.php?page=home, what I want to do is convert them to the nicer urls.
James
+1  A: 

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

Palantir
Would search engine's pick up these pages as http://example.com/i/am/here tho or will they still see index.php?q=/i/am/here?
James
Palantir
+1  A: 

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.

svenkubiak
Hi, thanks for the link to that generator that will come in handy! However, the mod_rewrite rules aren't really what I am referring to, the website links on the pages are currently dynamically generated, so I need to now parse them and replace them with nicer links instead
James
With "dynamically" you mean the variables like index.php?m=value1 or index.php?m=value2? If you have the name of the variable (e.g. "m") you can put that in the .htaccess with a nice rewrite rule. You can even use dynamic variables (e.g. m, page, bla, foo, etc.), but this requires a more complex rewrite rule. This can not be done with the generator i posted. Here's a good Cheat Sheet for that: http://www.addedbytes.com/apache/mod_rewrite-cheat-sheet/
svenkubiak
What I mean is, there are include files i.e. header.php/footer.php etc which have links to various pages on the website. They are currently being generated via looking up the database and working out the parameters to add on (index.php?page=home). However, because I now trying to move away from these types of URLs I need to update my links to dynamically create nicer urls. I understand I need the rewrite rules in place, however, I need to be able to parse these old generated urls and replace them with the nice ones.
James
+1  A: 

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

Skilldrick
Yes this is exactly my issue! Say I wanted to follow a similar approach to SO, where they add the extra question title onto the page (for SEO purposes). How would I go about doing that?
James
The question title in the URL is just added, but isn't used by SO. If you rename a question in a URL it still works: http://stackoverflow.com/questions/1518896/parsing-urls-using-java
Skilldrick
So as long as you write your rewrite rules correctly, you can append whatever you want to your links, and it'll still work the same.
Skilldrick
Yeah that was what I was meaning, all I need to do is ensure I have the correct rewrite rule and I can basically put any text after the /ModuleType/Id/Category-Title?
James
Absolutely. As long as it's not in a capturing group, it won't make it into the ugly URL, so it can be whatever.
Skilldrick
Thanks this has helped me a lot!
James