Currently, I have a simple PHP/htaccess setup where a single index.php can take incoming url and include files based on rewritten query string:
Options +FollowSymlinks -Indexes
RewriteEngine On
# Router:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?page=$1 [L,QSA]
The above works great when my index file is at the root of the server:
… the above would pass ?page=about/ to my PHP script, and I’d include the “about.php” from there. Simple enough.
Here’s what I’d like to do:
http://foo.com/2012/about/
http://foo.com/2013/about/
http://foo.com/about/ (this is actually housed in the 2014 folder).
http://foo.com/2014/about/ (this will remove 2014 from URL but keep about/)
Where, at the root of my server, I have:
2012/index.php
2013/index.php
2014/index.php
… and within each folder will be the index.php file that handles requests.
Here’s the tricky part: As you can see above, I only want the current year to be removed from the URL.
What would be the best way to handle this?
Here’s what I’ve got so far (not working):
# http://stackoverflow.com/a/10427485/922323
# http://www.askapache.com/htaccess/mod_rewrite-variables-cheatsheet.html
AddDefaultCharset utf-8
Options +FollowSymlinks -Indexes -MultiViews
RewriteEngine on
RewriteBase /
# Block access to hidden files:
RewriteCond %{SCRIPT_FILENAME} -d [OR]
RewriteCond %{SCRIPT_FILENAME} -f
RewriteRule "(^|/)\\." - [F]
# Force trailing slash:
RewriteCond %{REQUEST_URI} /+[^\\.]+$
RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]
# If a request tries to access the current year directly, then redirect
# it to its canonical version:
RewriteCond %{THE_REQUEST} 2014/
RewriteRule ^2014/(.*)$ http://%{HTTP_HOST}/$1 [R=301,L]
# .. and map any non-year requests to current year:
RewriteCond %{REQUEST_URI} !201(2|3|4)/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /2014/?page=$1 [L,QSA]
# Router:
RewriteCond %{REQUEST_URI} ([0-9]{4})/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /%1/index.php?page=$1 [L,QSA]
I’ve tried a number of combinations … At first, it was my hope that I could just put sub-htaccess files inside each year folder to handle the index.php part, but that seemed to trump the rewrite rules found in the parent directory.
I feel like I’m getting close.
The above currently has two problems:
- The root index request doesn’t work.
- The /2013 and /2012 does not pass $page as a query string.
With that said, can anyone give me pointers on the best way to handle this situation?
TIA!