mod_rewrite jiggery-pokery
Aug. 3rd, 2006 09:30 pm![[personal profile]](https://www.dreamwidth.org/img/silk/identity/user.png)
I got someone complaining that the menus on my site didn't load one too many times. They're generated by a little bit of Javascript, which tends to die on IE because people just turn off any and all scripting 'cause it's so full of security holes.
So I've converted it all to use a bit of PHP that generates the menu on the server end. This causes the problem that all my URLs will change, because I have to have everything be filename.php for this to work... or does it?
Not after a little fooling around with mod_rewrite, it doesn't! I shoved this in my .htaccess, above the code that prettifies the URLs to my gallery:
Translated into English, this tells the web server to do this:
If the user asks for a file whose name ends in .html, and if there's a .php file with the same name, give them the .php file instead. And stop doing the rewriting thing.
Yeah, I know, I could just stick the damn menu code in every file, but that means changing and re-uploading every file on my website when I change what's in the menu. It's a lot easier this way. Really. At least it is when you're a geek like me whose teeth grind at the thought of doing something by hand several times!
So I've converted it all to use a bit of PHP that generates the menu on the server end. This causes the problem that all my URLs will change, because I have to have everything be filename.php for this to work... or does it?
Not after a little fooling around with mod_rewrite, it doesn't! I shoved this in my .htaccess, above the code that prettifies the URLs to my gallery:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} ^(.*)\.html$
RewriteCond %1.php -f
RewriteRule ^(.*)\.html$ $1.php [ne,nc,l]
Translated into English, this tells the web server to do this:
If the user asks for a file whose name ends in .html, and if there's a .php file with the same name, give them the .php file instead. And stop doing the rewriting thing.
Yeah, I know, I could just stick the damn menu code in every file, but that means changing and re-uploading every file on my website when I change what's in the menu. It's a lot easier this way. Really. At least it is when you're a geek like me whose teeth grind at the thought of doing something by hand several times!
no subject
Date: 2006-08-04 01:35 am (UTC)no subject
Date: 2006-08-04 02:15 am (UTC)no subject
Date: 2006-08-04 08:44 am (UTC)If I were a chimp and this update was a locked box, I would be all curiosity yet unable to open it wiothout throwing it against a rock.
*eeek*
no subject
Date: 2006-08-05 06:10 am (UTC)no subject
Date: 2006-08-05 06:15 am (UTC):^)
no subject
Date: 2006-08-04 09:09 am (UTC)I got even lazier last week when I redesigned lazycat.org, again with mod_rewrite magic. The idea was to never have to change content files to update includes or other non-content things:
RewriteEngine On
RewriteRule ^$ index.html [QSA]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule (.*) - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) view.php [QSA,L]
So basically all URLs instead get sent to view.php (which reads the URL, maps it to some layout-less html/php fragment in another directory - or to a 404 page - and displays it in the layout), and you can override by making an actual file. Cheap-ass pseudo-CMS. :)
Actually I'm pretty sure the last two RewriteConds are superfluous, but I don't dare touch it now that it works!
no subject
Date: 2006-08-04 03:08 pm (UTC)no subject
Date: 2006-08-04 03:25 pm (UTC)no subject
Date: 2006-08-04 09:47 pm (UTC)I dunno if I could get by with something quite that simple and ubiquitous; I have some basic design rules but each page of my site has a lot of unique layout going on. Going to a minimal CMSoid thing might be slated for the next redesign, which is probably coming in, oh, 2011 or so, if I keep this one as long as I kept my old site. (Which generates the headers with a little cgi-bin I wrote in C.)