.htaccess tutorial

htaccess Elite


RewriteCond

Ask your mod_rewrite and Redirection questions here, and get answers!

Need some help with REWRITE Condition

New postby allstar784 » 27 Feb 2007 16:13

I understand how to place the or, however here is my situation. I have several categories over 500 that are dynamically set so the url looks similar to

Code: Select all
http://www.mydomain.com/folder/index.php?1catid=60453


But what I want is urls that look like:

Code: Select all
http://www.mydomain.com/folder/foods-groceries/


Since I know the titles of all the categories I figure I can use a condition to pretty much match up to whatever category # is displayed and display and SEO friendly url like above.

If I had an example of how I would rewrite those categories to my .htacces I could complete the thing myself. I just don't understand how I am to rewrite it.

Please help!

So to summarize: if index.php?catid=1065, then display /food-grocery
or if index.php?cat=10000 then display /chicken

Thanks so much!

Jori
allstar784
 
Posts: 1
Joined: 27 Feb 2007 15:57
Location: Chicago, IL

Re: Need some help with REWRITE Condition

New postby produke » 28 Feb 2007 04:27

allstar784 wrote:I understand how to place the or, however here is my situation. I have several categories over 500 that are dynamically set so the url looks similar to

Code: Select all
http://x.com/folder/index.php?1catid=60453


But what I want is urls that look like:

Code: Select all
http://x.com/folder/foods-groceries/


So to summarize: if index.php?catid=1065, then display /food-grocery
or if index.php?cat=10000 then display /chicken


Try this allstar784,

Code: Select all
RewriteEngine On
RewriteBase /
RewriteRule ^folder/foods-groceries/$ /folder/index.php?catid=1065 [NC,L]
RewriteRule ^folder/chicken/$ /folder/index.php?catid=10000 [NC,L]


That means that when a visitor to your site goes to the url http://x.com/folder/foods-groceries/ they will really be seeing whatever /folder/index.php?catid=1065 returns.

Likewise http://x.com/folder/chicken/ --> /folder/index.php?catid=10000


If you have more than 100 categories than you should use php as the main workhorse.. First you would redirect every url that is in the /folder/ that is a directory /anything/ to your /folder/index.php script.

Code: Select all
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/index.php.*$
RewriteRule ^folder/([^/]+)/$ /folder/index.php?cat=$1 [NC,QSA,L]


Then in index.php you would do something like this.

Code: Select all
<?php

$Category_Name = $_GET['cat'];

$Category_Array = array(
'foods-groceries' => '1065',
'chicken' => '10000'
);

$Category_ID = $Category_Array[$Category_Name];

?>
User avatar
produke
 
Posts: 242
Joined: 25 Sep 2006 04:48


Return to Redirect or Rewrite Questions