Jump to content

mod_rewrite - Rewrite rul for passing 2 folder names


Benster

Recommended Posts

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.

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

#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 (/)

 

:)

 

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.