Rewrite rule to control entire site?

Need help installing PHP, configuring a script, or configuring a server? Then come on in and post your questions! We'll try to help the best we can!

Moderator: General Moderators

Post Reply
User avatar
waradmin
Forum Contributor
Posts: 240
Joined: Fri Nov 04, 2005 2:57 pm

Rewrite rule to control entire site?

Post by waradmin »

I am currently working on a website project, and the page data for every page is stored in a database (HTML markup, etc). Then, when the user goes to 'www.something.tld/somepage.php' a query is executed that looks in the database for the page that has the field requri that matches '/somepage.php' and then displays the contents from the database. Same goes for if they go to 'www.something.tld/Somedepartment/someDeptPage.php', a query is executed that takes the request URI '/Somedepartment/someDeptPage.php' and finds the matching requri in the database then displays the content. That entire part works great, however....

I was wondering if it was possible (using rewrite rules) to just have a SINGLE php page (ie index.php) but make it appear the structure of department folders and pages still exists? So if a user goes to http://www.something.tld/Somedepartment ... ptPage.php for example, it would really just display the /index.php file but use the URI /Somedepartment/someDeptPage.php so it looks like the structure is there, however all of the files are not required.

I thought about just doing query strings in the URL (like ?page=___________) but that change is too drastic for the current site, as the structure of the URL's cant change.
www.WeAnswer.IT
Forum Newbie
Posts: 24
Joined: Wed Mar 19, 2008 6:33 pm

Re: Rewrite rule to control entire site?

Post by www.WeAnswer.IT »

Yes, this is possible. Put this in .htaccess:

Code: Select all

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^(.*)$ index.php?req=$1 [NS,L]
So if someone goes to: http://www.something.tld/somepage.php
your server will show them: index.php?req=somepage.php

If someone goes to: http://www.something.tld/somedepartment/somepage.php
your server will show them: index.php?req=somedepartment/somepage.php



If you want to get rid of the .php at the end, you can use this:

Code: Select all

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^(.*)\.php$ index.php?req=$1 [NS,L]
So if someone goes to: http://www.something.tld/somepage.php
your server will show them: index.php?req=somepage

If someone goes to: http://www.something.tld/somedepartment/somepage.php
your server will show them: index.php?req=somedepartment/somepage



You would then use index.php to split $_GET['req'] into departments and pages. If you need help with that let me know, or if you have problems with the mod_rewrite stuff above, let me know. I'm not able to test it now.
Post Reply