Note that there are some explanatory texts on larger screens.

plurals
  1. POSite structure with user friendly urls
    text
    copied!<p>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.</p> <p>Ok, so im making a site with a navigation structure resembling this -</p> <h3>Navigation Structure</h3> <pre><code>Home Products Overview Feature sub-page Feature sub-page Feature sub-page Videos Demonstrations Training ... </code></pre> <p>So my idea so far is to have for a file structure:</p> <h3>File Structure</h3> <pre><code>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 ... </code></pre> <p>So Im using a config.php with path variables to maintain links. And for each 'category' im using </p> <pre><code>pages/CATEGORY/index.php?page=XXX </code></pre> <p>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:</p> <pre><code>products home page - mysite/products/ products video page - mysite/videos OR mysite/products/videos products sub-pages - mysite/products/sub-page </code></pre> <p>With the ability to mask my actual file structure and when pages like products/index.php has $_GET variables, to tanform the url into:</p> <pre><code>mysites/products/sub-page </code></pre> <p>Ive looked at mod_rewrite of course, and was thinking something like </p> <pre><code>RewriteRule products/sub-page products/index.php?page=sub-page-name </code></pre> <p>might work but wouldnt answer masking folder structure (ie. 'pages'). This would require at least one rule for each 'category' with regex</p> <p>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</p> <p>Note: I also need to support 404 pages into the sub-pages, so that if I do use </p> <pre><code>index.php?page=sub-page-name </code></pre> <p>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)</p> <p>-EDIT-</p> <p>So the solution im looking at now is:</p> <pre><code>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 </code></pre> <p>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)</p> <p>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 </p> <pre><code>RewriteRule ^(.*)/products/info/(.*)$ http://.../public_html/pages/products/index.php?product=XXX </code></pre> <p>And even if I let the user pass in the variable '?product' themselves, would I write</p> <pre><code>RewriteRule ^(.*)/products/?product(.*)$ http://.../public_html/pages/products/index.php?product=$1 </code></pre> <p>or similar?</p> <p>Also, I need this to work both ways, so that if they use the full url (ie. with <code>?page</code>) it changes to the masked url</p> <p>-EDIT-</p> <p>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:</p> <pre><code>$route['products/info/(:any)'] = "products/view_info/$1"; $route['(:any)/(:any)'] = "$1/view/$2"; $route['(:any)'] = "$1/view"; </code></pre> <p>which, when combined with this in my .htaccess:</p> <pre><code>RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule .* index.php/$0 [PT,L] </code></pre> <p>solves my issues.</p> <p>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</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload