gerkintrigg Posted January 5, 2008 Share Posted January 5, 2008 Ooh now Mod re-writing is a lovely wonder to behold, but unlike it's real-world counterparts (like sex and beer), it seems rather tricky to get the hang of. I'm trying to work out how to parse many variables to a URL and re-write the URL to get each variable. I worked out that bunging this into a .htaccess file will do SOME of what I need: RewriteRule ^change_event/([^/\.]+)/?$ diaries/change_event.php?id=$1 [L] but I want to be able to send other data too (like the date in YYYY-mm-dd format) but when I try, it just does something funny to the images and doesn't display them. I thought it may have been something to do with the PHP $root variable, but nope - all seems to be fine there. If you can think of anything, please let me know, because all the tutorials on line that I can find, end at around about where I am right now. ??? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/84617-solved-mod-rewrite/ Share on other sites More sharing options...
Wuhtzu Posted January 5, 2008 Share Posted January 5, 2008 mod-rewrite can be quite tricky to work with but when you get the hang of it, it is quite easy. And a part of getting it is to start out with the proper syntax and "tools": #1: The "syntax" of your .htaccess file should be: RewriteEngine on RewriteBase /path/from/your/webroot/to/your/rewrite/script/ RewriteRule ^([a-z]+)/([a-z]+)$ script.php?key1=$1&key2=$2 RewriteEngine on turns on the rewrite engine (if you have permissions to) RewriteBase /path/from/ sets the rewrite base. If domain.com points at your webroot and you want to do rewriting at domain.com/test/products/script.php (obtaining e.g. domain.com/test/products/6123) then you should set your rewritebase to "/test/products/". You save your self a lot of headaches by doing this. RewriteRule ^([a-z]+)/([a-z]+)$ script.php?key1=$1&key2=$2 the rewriterule uses regular expression to match part of the url (from your rewritebase and forward / reading left to right) and "saves" those matches as $1, $2, $3 ... $n which you can then stick in your script.php?key1=$1&key2=$2&keyn=$n. #2: Realise that you have the full power of regular expression at your disposal when you use mod-rewrite. Syntax of the rewriterule is: RewriteRule ^(pattern1)/(pattern2)/(pattern3)$ script.php?key1=$1&key2=$2&key3=$3 RewriteRule ^(pattern1),(pattern2),(pattern3)$ script.php?key1=$1&key2=$2&key3=$3 RewriteRule ^(pattern1)-(pattern2)-(pattern3)$ script.php?key1=$1&key2=$2&key3=$3 Where / , and - are delimiters separating the patterns to match. Of course ^ and $ are optional - they just anchor the matching to start at the beginning of the url (from your rewritebase an onwards) and end at the end. I can't think of a scenario where you wouldn't want to use them. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Now on to your "question" where you wanted to create some more "advanced" rewriting rules. Take a look at the following .htaccess: # URL rewriting RewriteEngine on RewriteBase /sandbox/rewrite/ RewriteRule ^([0-9]{4}-[0-9]{2}-[0-9]{2})/([a-z]+)/([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})$ rewrite.php?date=$1&string=$2&ip=$3 RewriteRule ^([0-9]{4}-[0-9]{2}-[0-9]{2}),([a-z]+),([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})$ rewrite.php?date=$1&string=$2&ip=$3 RewriteRule ^([0-9]{4}-[0-9]{2}-[0-9]{2})-([a-z]+)-([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})$ rewrite.php?date=$1&string=$2&ip=$3 rewrite.php: <pre> <?php print_r($_GET); ?> </pre> I am doing my rewriting on this script http://wuhtzu.dk/sandbox/rewrite/rewrite.php and therefore my rewritebase is "/sandbox/rewrite/". Try the script here: http://wuhtzu.dk/sandbox/rewrite/1998-12-06/lol/123.456.789.000 http://wuhtzu.dk/sandbox/rewrite/1998-12-06,commadelimiter,123.456.789.000 http://wuhtzu.dk/sandbox/rewrite/1998-12-06-anotherdelimiter-123.456.789.000 As you can see my .htaccess file contains 3 almost identical rewriterules, they only differ by the delimiter (/ , -) and the rewrite rules each consists of 3 patterns: ([0-9]{4}-[0-9]{2}-[0-9]{2}) which matches a date formatted as yyyy-mm-dd ([a-z]+) which matches a string longer that 1 char ([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}) which matches an IP address You should be able to recognize the very basic perl style regex. Hope this helps you Wuhtzu EDIT: a few typos Quote Link to comment https://forums.phpfreaks.com/topic/84617-solved-mod-rewrite/#findComment-431182 Share on other sites More sharing options...
gerkintrigg Posted January 6, 2008 Author Share Posted January 6, 2008 Thanks, that's very helpful. I'm still rather confused as to why the images are not being updated in the same way as the URL though. Quote Link to comment https://forums.phpfreaks.com/topic/84617-solved-mod-rewrite/#findComment-431727 Share on other sites More sharing options...
Wuhtzu Posted January 6, 2008 Share Posted January 6, 2008 And I'm still confused about your images it just does something funny to the images and doesn't display them. You explain almost less than nothing about your problem with some images. Your initial post made it sound like you had trouble making a rewrite rule that would accept for example a date yyyy-mm-dd. Please explain some more about those images, whats wrong with them and what should they do / look like! Quote Link to comment https://forums.phpfreaks.com/topic/84617-solved-mod-rewrite/#findComment-431823 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.