jacko310592 Posted December 4, 2009 Share Posted December 4, 2009 hey guys, im completely new to the .htaccess file, i need to find a way to rewrite URLs such as: http://localhost/gallery/album.php?alternative http://localhost/gallery/album.php?edits http://localhost/gallery/album.php?portraiture etc etc to: http://localhost/gallery/album/alternative http://localhost/gallery/album/edits http://localhost/gallery/album/portraiture can anyone suggest a way this can be done? ive searched the net but what i find seems to be a bit confusing thanks guys Quote Link to comment Share on other sites More sharing options...
mrMarcus Posted December 4, 2009 Share Posted December 4, 2009 RewriteEngine On RewriteBase / RewriteRule ^gallery/album\.php\?([a-zA-Z]+)$ gallery/album.php?$1 [NC,L] try that. Quote Link to comment Share on other sites More sharing options...
jacko310592 Posted December 4, 2009 Author Share Posted December 4, 2009 hey mrMarcus, thanks for the reply, but with that code i just recieve a 404 error :/ any ideas? + would i be right in asuming that the code needed to be place within the .htaccess file within the root dir and no where else? thanks Quote Link to comment Share on other sites More sharing options...
mrMarcus Posted December 4, 2009 Share Posted December 4, 2009 wow. i had a total brain fart with that code. try this: RewriteEngine On RewriteBase / RewriteRule ^gallery/album/([a-zA-Z]+)/?$ gallery/album.php?page=$1 [NC,L] EDIT: to explain the code a little bit: the ([a-zA-Z]+) denotes a range where only characters a-z (as well as any uppercase A-Z) are allowed .. numeric values will return a 404. the + denotes that anything is acceptable within that range. the /? at the end means that the URL can be entered with or without a trailing forward slash. if you kept the trailing slash and removed the ?, and then entered the same URL without a trailing slash, you would get a 404. this way, it does not matter if there is a trailing slash or not. and the $ after the /? means that we need an exact match, meaning that we must have a match in our URL must be exactly as follows: '/gallery/album/anAlbumNameHere' in the 2nd half of the rule, you will see a ?page=$1 .. the ?page works the exact same way as it would outside of .htaccess .. you call it like so: $_GET['page'] .. and you can attach more than just one, so: ?page=$1&id=$2&something=$3 will work granted you accommodate the $2 and $3 in the initial half of the rule .. you can go up to $9, and $0 returns the entire string. the [NC,L] are conditions .. NC stands for NoCase, so the URL in questions becomes CaSE-iNSeNsiTiVE, and the L stands for Last, meaning that if this rule is met, the script stops and no more rules are checked. hope you understand. and usage would be using the $_GET superglobal to check for 'page' .. so: create file in this structure: http://localhost/gallery/album.php <?php echo $_GET['page']; //will echo out whatever is in the URL after /album/ ?> for security reasons, you might want to create an array will is available to each page where you can check if that the page in question is ok, ie: <?php $arr = array ('alternative','edits','portraiture'); if (in_array ($_GET['page'], $arr)) { echo $_GET['page']; } else { echo 'Incorrect page error goes here.'; } ?> obviously a very simple example, but it's the jist. and yes, that code goes into an .htaccess file, and can be placed in the root directory which makes it available to any directory within the root structure. make sure when saving an .htaccess file that you select "All Files" or something similar and NOT ".txt Text Document", as the .htaccess file be appended with a .txt (.htaccess.txt) and it will not work. any questions? Quote Link to comment Share on other sites More sharing options...
jacko310592 Posted December 5, 2009 Author Share Posted December 5, 2009 thank you, the code worked greatly (: Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.