Benster Posted January 6, 2008 Share Posted January 6, 2008 Hi, Im struggling with a rewrite rule. Heres what im looking to do: www.mydomain.com/foo/myFolder/bar => /test.php?foo=xxx&bar=xxx Note:"myFolder" name is static will not change however foo and bar will. So currently I have: Code: RewriteRule ^([^/]+)/myFolder/?$ /test.php?bar=$1 [QSA,L] Can anyone advise on how to pass through the first folder variable (foo) also? Best regards, Ben. Quote Link to comment https://forums.phpfreaks.com/topic/84753-mod_rewrite-rewrite-rul-for-passing-2-folder-names/ Share on other sites More sharing options...
Wuhtzu Posted January 6, 2008 Share Posted January 6, 2008 First of all may I refer you to this post: http://www.phpfreaks.com/forums/index.php/topic,175754.msg778928.html#msg778928 - just because it is somewhat informative. Secondly the rewrite rule you are after is pretty simple: .htaccess: # URL rewriting RewriteEngine on RewriteBase /sandbox/rewrite/ RewriteRule ^([a-zA-Z0-9]+)/myFolder/([a-zA-Z0-9]+)/$ foobar.php?foo=$1&bar=$2 foobar.php: <pre> <?php print_r($_GET); ?> </pre> The rewritebase is set to /sandbox/rewrite/ because I did this rewrite test at http://wuhtzu.dk/sandbox/rewrite/foobar.php - you can try it here and see if it does what you expected: http://wuhtzu.dk/sandbox/rewrite/foo/myFolder/bar/ Change the rewritebase to what ever suits your folder / file structure Quote Link to comment https://forums.phpfreaks.com/topic/84753-mod_rewrite-rewrite-rul-for-passing-2-folder-names/#findComment-431904 Share on other sites More sharing options...
Benster Posted January 6, 2008 Author Share Posted January 6, 2008 Hi Wuhtzu, Many thanks, your comments and other post were both very helpful. I now have 2 more questions im afraid! 1) Is RewriteBase essential or good practice? I have not been using it so far as im always rewriting from the root i.e. /. 2) Is there a simple addition to the rule that can cater for a missing final /? E.g. , if i try the url - http://wuhtzu.dk/sandbox/rewrite/test/myFolder/test2 It will fail, however if i add a training slah its good. I can do this with php if needed, however im keen to see if there is an addition to the rule? Thanks again for your time, im starting to understand this beast!! Regards, Ben. Quote Link to comment https://forums.phpfreaks.com/topic/84753-mod_rewrite-rewrite-rul-for-passing-2-folder-names/#findComment-431993 Share on other sites More sharing options...
Wuhtzu Posted January 6, 2008 Share Posted January 6, 2008 #1: I will personally call RewriteBase good practice since your rewrite rules looks cleaner / more simple and you can easily apply a change in your directory structure to all your rules without having to change each rule. #2: As I said in the post I posted a link to, you have the full power of regex as your disposal, so you can simply make the trailing slash (/) optional: RewriteRule ^([a-zA-Z0-9]+)/myFolder/([a-zA-Z0-9]+)/?$ foobar.php?foo=$1&bar=$2 The question mark (?) means 0 or 1 instance of the preceding character. So 0 or 1 instance of the trailing slash (/) Quote Link to comment https://forums.phpfreaks.com/topic/84753-mod_rewrite-rewrite-rul-for-passing-2-folder-names/#findComment-432033 Share on other sites More sharing options...
Benster Posted January 7, 2008 Author Share Posted January 7, 2008 Superb, thats much clearer. Thanks again for the assistance, its very much appreciated. Ben. Quote Link to comment https://forums.phpfreaks.com/topic/84753-mod_rewrite-rewrite-rul-for-passing-2-folder-names/#findComment-432554 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.