Help mod_rewrite needed for directory script

Discuss practical ways rearrange URLs using mod_rewrite.

Help mod_rewrite needed for directory script

Postby marky9378 » Fri Jul 04, 2008 12:51 am

Hi

I am looking to create friendly urls for this directory on my website.

http://www.aroundgloucester.co.uk/busin ... stype=catg

I need the whole of this folder http://www.aroundgloucester.co.uk/business-directory/ and the contents to be search engine friendly urls

Please Please Help Me
marky9378
 
Posts: 9
Joined: Thu Jul 03, 2008 4:17 pm

Postby richardk » Fri Jul 04, 2008 2:27 pm

If you provide examples of the current URL and the URL you want, i can help you.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby marky9378 » Sat Jul 05, 2008 1:57 am

marky9378
 
Posts: 9
Joined: Thu Jul 03, 2008 4:17 pm

Postby marky9378 » Sat Jul 05, 2008 4:08 am

I have a forum on this website which has a .htaccess using php to automatically convert into friendly uls.

Could someone tell me how i can use this on the directory above script.

PHP

<?php

/**
* Prepares an URL for URL Rewriting
*
* @param string $type
* @param int $id
* @param string $name
* @return string
* @author Dale Smith <info@wwwthreads.com>
*/

function makeurl($type, $id, $name)
{
$url = strtr($name,'ÀÁÂÃÄÅàáâãäåÒÓÔÕÖØòóôõöøÈÉÊËèéêëÇçÌÍÎÏìíîïÙÚÛÜùúûüÿÑñ/','AAAAAAaaaaaaOOOOOOooooooEEEEeeeeCcIIIIiiiiUUUUuuuuyNn-');
$url=preg_replace('/ /', '-', $url);
$url=trim(preg_replace('/[^a-z|A-Z|0-9|-]/', '', strtolower($url)), '-');
$url=preg_replace('/\-+/', '-', $url);
$url = urlencode($type . $id .'-'. $url .'.html');
return $url;
}

function replace_for_mod_rewrite(&$s) {
$urlin = array(
"/\"([^.]*).php\?([a-z0-9_]*)=([a-zA-Z0-9_.]*)((&amp;)|&)+([a-z0-9_]*)=([a-zA-Z0-9_.]*)((&amp;)|&)+([a-z0-9_]*)=([a-zA-Z0-9_.]*)\"/"
, "/\"([^.]*).php\?([a-z0-9_]*)=([a-zA-Z0-9_.]*)((&amp;)|&)+([a-z0-9_]*)=([a-zA-Z0-9_.]*)\"/"
, "/\"([^.]*).php\?([a-z0-9_]*)=([a-zA-Z0-9_.]*)\"/"
, "/\"([^.]*).php\"/"
);
$urlout = array(
"\"\\1-\\2-\\3-\\6-\\7-\\10-\\11.html\""
, "\"\\1-\\2-\\3-\\6-\\7.html\""
, "\"\\1-\\2-\\3.html\""
, "\"\\1.html\""
);
$s = preg_replace($urlin, $urlout, $s);
return $s;
}

function replace_for_mod_rewrite1(&$s) { //debug!
$urlin = array(
"/([^.]*).php\?([a-z0-9_]*)=([a-zA-Z0-9_.]*)[(&amp;)|&]+([a-z0-9_]*)=([a-zA-Z0-9_.]*)[(&amp;)|&]+([a-z0-9_]*)=([a-zA-Z0-9_.]*)([^'])/"
, "/([^.]*).php\?([a-z0-9_]*)=([a-zA-Z0-9_.]*)[(&amp;)|&]+([a-z0-9_]*)=([a-zA-Z0-9_.]*)([^'])/"
, "/([^.]*).php\?([a-z0-9_]*)=([a-zA-Z0-9_.]*)([^'])/"
, "/([^.]*).php([^'])/"
);
$urlout = array(
"\\1-\\2-\\3-\\4-\\5-\\6-\\7.html\\8"
, "\\1-\\2-\\3-\\4-\\5.html\\6"
, "\\1-\\2-\\3.html\\4"
, "\\1.html\\2"
);
$s = preg_replace($urlin, $urlout, $s);
return $s;
}

/*
function replace_for_mod_rewrite2(&$s) { //slow?
$urlin = array(
"/([^.]*).php\?([a-z0-9_]*)=([a-zA-Z0-9_.]*)[(&amp;)|&]+([a-z0-9_]*)=([a-zA-Z0-9_.]*)[(&amp;)|&]+([a-z0-9_]*)=([a-zA-Z0-9_.]*)/"
, "/([^.]*).php\?([a-z0-9_]*)=([a-zA-Z0-9_.]*)[(&amp;)|&]+([a-z0-9_]*)=([a-zA-Z0-9_.]*)/"
, "/([^.]*).php\?([a-z0-9_]*)=([a-zA-Z0-9_.]*)/"
, "/([^.]*).php/"
);
$urlout = array(
"\\1-\\2-\\3-\\4-\\5-\\6-\\7.html"
, "\\1-\\2-\\3-\\4-\\5.html"
, "\\1-\\2-\\3.html"
, "\\1.html"
);
$s = preg_replace($urlin, $urlout, $s);
return $s;
}
*/
function replace_for_mod_rewrite2(&$s) { //slow?
$urlin = array(
"/([^.]*).php\?([a-z0-9_]*)=([a-zA-Z0-9_.]*)((&amp;)|&)+([a-z0-9_]*)=([a-zA-Z0-9_.]*)((&amp;)|&)+([a-z0-9_]*)=([a-zA-Z0-9_.]*)/"
, "/([^.]*).php\?([a-z0-9_]*)=([a-zA-Z0-9_.]*)((&amp;)|&)+([a-z0-9_]*)=([a-zA-Z0-9_.]*)/"
, "/([^.]*).php\?([a-z0-9_]*)=([a-zA-Z0-9_.]*)/"
, "/([^.]*).php/"
);
$urlout = array(
"\\1-\\2-\\3-\\6-\\7-\\10-\\11.html"
, "\\1-\\2-\\3-\\6-\\7.html"
, "\\1-\\2-\\3.html"
, "\\1.html"
);
$s = preg_replace($urlin, $urlout, $s);
return $s;
}

?>

.HTACCESS

RewriteEngine on
RewriteRule ^f([0-9]+)(.*).html$ view_forum.php?id=$1 [L]
RewriteRule ^t([0-9]+)(.*).html$ view_topic.php?id=$1 [L]
RewriteRule ^p([0-9]+)(.*).html$ view_topic.php?pid=$1 [L]
RewriteRule ^l([0-9]+)(.*).html$ view_poll.php?pid=$1 [L]
####### extra
RewriteRule ^(.*)spell.html$ $1spell.html [L]
RewriteRule ^(.*)-(.*)-(.*)-(.*)-(.*)-(.*)-(.*).html$ $1.php?$2=$3&$4=$5&$6=$7 [L]
RewriteRule ^(.*)-(.*)-(.*)-(.*)-(.*).html$ $1.php?$2=$3&$4=$5 [L]
RewriteRule ^(.*)-(.*)-(.*).html$ $1.php?$2=$3 [L]
RewriteRule ^(.*).html$ $1.php [L]
#######
RewriteRule ^(.*).rss$ extern.php?action=$1&type=rss [L]
RewriteRule ^blabla.html$ index.php [L]

Please Help
marky9378
 
Posts: 9
Joined: Thu Jul 03, 2008 4:17 pm

Postby richardk » Mon Jul 07, 2008 2:23 pm

/business-directory/index.php?stype=bus&catgID=7&catgname=Accountants

i want this one to be
/business-directory/accountants.htm

/business-directory/index.php?stype=bus&catgID=22&catgname=Antiques%20And%20Collectables

I want this one to be
/business-directory/anitques-and-collectables.htm

If you want it that short (without the stype or catgID) will need individual rules for each one
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteRule ^accountants\.html$               ./index.php?stype=bus&catgID=7&catgname=Accountants                    [QSA,L]
RewriteRule ^anitques-and-collectables\.html$ ./index.php?stype=bus&catgID=22&catgname=Antiques%20And%20Collectables [QSA,L]

Or you need to read how to change a numeric ID into a name/title. It's the same idea, except you could have two values to find not one.

Or you can have the stype and catgID in the URL (maybe not the stype if it doesn't change), for example for /business-directory/bus/22-Antiques+And+Collectables.htm
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteRule ^([^/]+)/([0-9]+)-([^/]+)\.html$ ./index.php?stype=$1&catgID=$2&catgname=$3 [QSA,L]


Does stype change? How many different values for stype are there?

You will have to edit your PHP to output these new URLs.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby marky9378 » Tue Jul 08, 2008 1:30 am

when i add a new business a new stype is created but i do not know php that well and i would not know where to start
marky9378
 
Posts: 9
Joined: Thu Jul 03, 2008 4:17 pm

Postby marky9378 » Tue Jul 08, 2008 2:12 pm

i have found this in the php file

while ($row = getrsrow($resultSet)) {
print ("<div class=\"catlistitem\"><a href=\"" . $_SERVER['PHP_SELF'] . "?stype=bus&catgID=" . $row['categoryID'] . "&catgname=" . $row['categoryname'] . "\">" . $row['categoryname'] . "</a></div>\n");

Where do i edit this
marky9378
 
Posts: 9
Joined: Thu Jul 03, 2008 4:17 pm

Postby richardk » Wed Jul 09, 2008 2:01 pm

You edit
Code: Select all
<a href=\"" . $_SERVER['PHP_SELF'] . "?stype=bus&catgID=" . $row['categoryID'] . "&catgname=" . $row['categoryname'] . "\">


You probably want
Code: Select all
<a href=\"bus/" . $row['categoryID'] . "-" . str_replace(' ', '+', $row['categoryname']) . ".html\">
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby marky9378 » Thu Jul 10, 2008 2:26 am

Lets through a party

Richard Thank You so much for your help

I have done it Thank you for helping me
marky9378
 
Posts: 9
Joined: Thu Jul 03, 2008 4:17 pm


Return to Friendly URLs with Mod_Rewrite

Who is online

Users browsing this forum: No registered users and 43 guests

cron