Doing a URL Re-Write While Using PHP Get Method

Hello,

When I click on a comment section for a given entry on a site I have, the URL looks like this:

http://www...com/.../comments/index.php?submission=Portugal%20Crushes%20North%20Korea&submissionid=62&url=nytimes.com/2010/06/22/sports/soccer/22portugalgame.html?hpw&countcomments=3&submittor=johnjohn12&submissiondate=2010-06-21%2019:00:07&dispurl=nytimes.com

I want to make it look like this URL:

http://www...com/.../comments/Portugal-Crushes-North-Korea-62

I understand that this involves adding rules to the .htaccess file. I have two questions:

  1. Since I am using the GET method in PHP, the ugly URL has a bunch of variables. I don’t want all of these variables to appear in the clean URL. Is it possible to only include a few of the variables in the clean URL but still have a rule directing it to an ugly URL with all of the variables?

  2. Once I have the rules written, do I go back and change the links in the source code to direct to the clean URLs? If so, how do I do this using the GET method when the clean URL does not have all of the variables that I want to pass along?

Thanks in advance,

John

Er, say wha’ now? :rolleyes:

  1. The format of the rule is completely upto you, and yes you need to use an .htaccess if using Apache or the rewrite module if IIS.

  2. Yes, you would need to alter the hyperlinks to suit, however your code should work exactly the same if your rewrite maps the variables to the same locations.

:slight_smile:

The best way to so this is with preg_replace and regular expressions
http://php.net/manual/en/function.preg-replace.php

RewriteEngine On
RewriteCond %{REQUEST_URI} !^\\/(folders|that|are|not|to|be|redirected)\\/?([a-z0-9\\_\\-\\/]+)$
RewriteRule ^([a-z0-9\\_\\-]+\\/)$ /comments/index.php [NC]

It’s good stuff :slight_smile:

then read the url, split at the “/” and you have all your variables

Sorry, preg_replace wasn’t needed, I’m a bit rusty in PHP as I’ve been working in ASP classic for a while :slight_smile: