I am trying to figure out Apache’s Mod Rewrite and so far it’s not working. Here is what I am trying to do.
I have an index.php in the root directory of my site that is a template for all pages. I want to be able to organize my files in directories and the query string will have slashes.
So…
http://www.example.com/page.html
should be…
http://www.example.com/index.php?url=page
and…
http://www.example.com/directory/page.html
should be…
http://www.example.com/index.php?url=directory/page
I’ve got that working but I want to be able to go n directories deep…
http://www.example.com/directory1/directory2/page.html
should be…
http://www.example.com/index.php?url=directory1/directory2/page
I know I could just put a bunch of rewrites for however many directories I deep I want to go, but is there a single line RewriteRule that will put anything after the first / will be considered a query string?
This is what I have so far…
RewriteEngine on
RewriteRule ^([a-z-]+)\.html$ index.php?url=$1
RewriteRule ^faculty/([a-z-]+)\.html$ index.php?url=faculty/$1
Behind the scenes, PHP is including the file that is in the location that the url is specified.
This is bad idea to include file which name was passed from user. At least implement very STRONG validation, otherwise prepare to be hacked.
If you still want to use this — here is the rule:
RewriteRule ^(.+)\.html$ index.php?url=$1 [QSA,L]