Okay, so I know this question title is a little vague and it has to have been answered before, but Ive spent hours looking at various solutions and none of them quite answer what im looking for.
Ok, so im making a site with a navigation structure resembling this -
Home
Products
Overview
Feature sub-page
Feature sub-page
Feature sub-page
Videos
Demonstrations
Training
...
So my idea so far is to have for a file structure:
resources/
config.php
public_html/
index.php
menu.php
head.php
footer.php
css/
images/
js/
pages/
home/
index.php
home.php
products/
index.php
sub-page.php
sub-page.php
videos.php
demonstrations.php
training/
index.php
...
So Im using a config.php with path variables to maintain links. And for each 'category' im using
pages/CATEGORY/index.php?page=XXX
What im really looking for is a way to structure my files or setup .htaccess so that it provides clean urls to look something like:
products home page - mysite/products/
products video page - mysite/videos OR mysite/products/videos
products sub-pages - mysite/products/sub-page
With the ability to mask my actual file structure and when pages like products/index.php has $_GET variables, to tanform the url into:
mysites/products/sub-page
Ive looked at mod_rewrite of course, and was thinking something like
RewriteRule products/sub-page products/index.php?page=sub-page-name
might work but wouldnt answer masking folder structure (ie. 'pages'). This would require at least one rule for each 'category' with regex
Just to clarify: all pages/categories/index.php contain all of the to level .php parts (head, menu, footer, ect) and the top level index.php just contains an include for pages/home/index.php
Note: I also need to support 404 pages into the sub-pages, so that if I do use
index.php?page=sub-page-name
And the sub-page doesnt actually exist, that it shows a valid message, or even just reverts to the default index.php content (conditional check on variables against existing files or 'approved values' list)
-EDIT-
So the solution im looking at now is:
RewriteRule ^(.*)/products/$ http://.../public_html/pages/products/index.php
RewriteRule ^(.*)/products/videos$ http://.../public_html/pages/products/index.php?page=videos
RewriteRule ^(.*)/products/demonstrations$ http://.../public_html/pages/products/index.php?page=demonstrations
But this only solves part of the problem (and is a bit too specific for my liking but i can bear with that if absolutely need be)
How about when im doing sub-pages, the number is fiarly large and potentially subject to change. Instead of doing 1 rule for each subpage could I use
RewriteRule ^(.*)/products/info/(.*)$ http://.../public_html/pages/products/index.php?product=XXX
And even if I let the user pass in the variable '?product' themselves, would I write
RewriteRule ^(.*)/products/?product(.*)$ http://.../public_html/pages/products/index.php?product=$1
or similar?
Also, I need this to work both ways, so that if they use the full url (ie. with ?page
) it changes to the masked url
-EDIT-
So im now using the CodeIgniter php framework, its a very light mvc framework which doesnt enforce too many strict rules upon you, it also allows me to place this in my routes.php:
$route['products/info/(:any)'] = "products/view_info/$1";
$route['(:any)/(:any)'] = "$1/view/$2";
$route['(:any)'] = "$1/view";
which, when combined with this in my .htaccess:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
solves my issues.
Im not using it too strickly right now, still have a mess of javascript controlling my youtubeapi requests within my videos.php page, but i may convert this to a model.php and jquery ajax hybred, if i can get my model.php to easily intrigrate without causing me to rewrite all of my api-handling code to php
Make the top index.php control all the pages. So /products/sub-page would be rewritten to /index.php?page=%2Fproducts%2Fsub-page. I think your rewrite rule would look something like this:
RewriteRule ^(.*)$ index.php?page=$1
Then your index.php would look something like this:
include( 'pages'.$_GET['page'] );
You may need to have a few rules ahead of it to exclude doing the rewrite for your css, images, and javascript.