views:

243

answers:

2

Currently I have a url like this

http://<client_name>.website.com/index.php?function_name&cat=32

I want to set things up so that our Marketing people can publish url's like

http://<client_name>.website.com/<parent_category>/<category>   

The "cat=XX" will be generated of the last <category> only. But marketing wants to use the parent category in their campaigns. Currently we pass all of URL's through index.php in the html root directory (this will become important later).

I've tried several solutions including:

  1. mod_rewrite - the problem with this approach is that it becomes a huge .htaccess file since we need to write a rule for each category.

  2. RewriteMap - this came pretty close since I could query the database to build map file for output. However, I've since learned we don't have access to httpd.conf.

  3. index.php - I've tried running everything through our index.php file which works, but doesn't keep the URL in the browser friendly for SEO purposes.

I'm hoping somebody has another idea which might help, I'd really appreciate it. If you've got a reference to something on the web that would help that'd be great too.

+2  A: 

Why not to route all requests to the index.php with mod_rewrite and use PHP to write the routing logic, which seems way more reliable way than writing distinct rewrite rules?

As simple .htaccess as this one

RewriteEngine on
RewriteBase /
RewriteCond  %{REQUEST_FILENAME} !-f
RewriteCond  %{REQUEST_FILENAME} !-d
RewriteRule  ^(.*)$ index.php?request=$1 [QSA,L]

And few lines of PHP code in the index.php

$client_name = strtok($_SERVER['HTTP_HOST'],".");
list ($cat,$subcat) = explode("/",trim($_GET['request'],"/"));
Col. Shrapnel
Yes, but I want to keep the URL as the pretty one, but need to pass it as the "cat=".
John Swaringen
@John Swaringen: The URL in the browser will remain the same. Only behind the scenes will this conversion take place.
webbiedave
I use the method described here. Mod rewrite to hide the index.php and then build a routing system in PHP - this lets you abstract the rules so that you can have separate routers for different sections of the site.Look at how Joomla handles routers - you specify a builder and parser. When parsing you simply loop through the various segments and assign each one to a value so you can still have "cat=blah"
Jarrod
+1  A: 

In addition to Col. Shrapnel's answer, at least slim the selection down:

RewriteRule ^/([-a-zA-Z0-9_]+)/([0-9]+)$ index.php?function_name=$1&cat=$2 [L,QSA]  # domain.com/cat_name/23 is sent to the server as domain.com/index.php?function_name=cat_name&cat=23
Dan Heberden