Today I spent hours trying to figure out something that I thought was going to be very straightforward and simple. Wrong! Part of the problem is I don’t work with regular expressions often so the idea of writing .htaccess files is sometimes mystifying to me.
All I really wanted to do seemed easy enough:
Redirect example.com/blog/?p=$variable to example.com/?p=$variable.
I just couldn’t figure out how to do it with .htaccess, and trial-and-error wasn’t a good idea since screwing up would throw the dreaded 500 Internal Server Error. I didn’t have much time so I finally threw in the .htaccess towel and decided to go with what I know – PHP.
Using $_GET can be dangerous if you don’t sanitize input, so strip_tags() helps ensure that the user wasn’t inputting malicious data. Your mileage may vary, and you may need more elaborate validation… have fun figuring that out.
Anyway, this code is ugly and really should be cleaned up, but it isn’t a bad place to start! My example only has one argument, but there’s no reason you couldn’t do this with more.
$postid = $_GET['p'];
$postid = strip_tags($postid);
header('Location: http://example.com/?p='.$postid);
If anybody knows what the “proper” way to do this via .htaccess is, please let me know!






