Forum Moderators: phranque

Message Too Old, No Replies

Another htaccess rewrite

multiple virtual dirs to strings

         

adamski

7:09 pm on Aug 18, 2009 (gmt 0)

10+ Year Member



Hi Again,

I'm trying to achieve the following:

/dir/dir1/dir1 > internal rewrite > index.php?var=$1&var2=$2&var3=$3

Sometimes the page called will be /dir/dir1 and sometimes /dir/dir1/dir2

Currently I have

RewriteCond %{REQUEST_URI} dir
RewriteRule ^(.+)/(.+)[/(.+)]$ index.php?var=$1&var2=$2&var3=$3 [L]
RewriteRule ^(.+)/(.+)/(.+)$ index.php?var=$1&var2=$2&var3=$3 [L]
RewriteRule ^(.+)/(.+)$ index.php?var=$1&var2=$2 [L]

Now I can get it to work for either the 2 or 3 vars but not for both. Obviously I'm not using all the above together I've been experimenting with.

A nudge in the right direction would be grand!

Thanks,

Adam

g1smd

8:16 pm on Aug 18, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The .+ pattern is "any character, one or more". Swap it out for something else that says "not a slash".

The [ and ] are completely wrong. Using that says the final part of the URL can only consist of / or ( or . or + or ) only.

Put the pattern that looks at the longer URLs first, and the shorter URLs last.

adamski

8:25 pm on Aug 18, 2009 (gmt 0)

10+ Year Member



Why would I be saying not a slash? I need that last slash before the last brackets. Yes [] line shouldn't be in there. I was trial and error testing as I've still not got my head round rewrite!

adamski

8:33 pm on Aug 18, 2009 (gmt 0)

10+ Year Member



Knew I'd find a thread similar if I looked hard enough. Solution that works is


RewriteRule ^([a-z0-9_%\ \-]+)/([a-z0-9_%\ \-]+)/([a-z0-9_%\ \-]+)?$ index.php?var=$1&var2=$2&var3=$3 [NC,NE,L]
RewriteRule ^([a-z0-9_%\ \-]+)/([a-z0-9_%\ \-]+)?$ index.php?var=$1&var2=$2 [NC,NE,L]

Don't know if this is the most efficient way or not though.

Thanks g1 for help.

g1smd

8:43 pm on Aug 18, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You cannot do trial and error with this stuff. You need to document exactly what the pattern should and should not match and then code that. You are playing with fire to guess any part of it.

The reason for "not a slash" is that you want to match all characters that are "not a slash" up until the next slash. That's a simpler pattern than listing all the valid characters.

adamski

8:50 pm on Aug 18, 2009 (gmt 0)

10+ Year Member



Yes that makes sense. I disagree about trial and error. Understanding errors and my mistakes helps me learn the right way of doing things!

g1smd

8:56 pm on Aug 18, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



OK. I'll rephrase that. Do all of your 'trial and error' stuff on your test server; never, ever, on a live site. :)

jdMorgan

8:57 pm on Aug 18, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> Understanding errors and my mistakes helps me learn the right way of doing things!

...as long as you're not doing it on a live server. If it's a live server, a single error can destroy your site's rankings for a month, or three months, or a year... :o

The regular expressions tutorial cited in our Apache Forum Charter may prove useful to you.

Jim

adamski

9:02 pm on Aug 18, 2009 (gmt 0)

10+ Year Member



Hell no! Dev server all the way until it's rock solid lol :p

Thanks both