Forum Moderators: phranque
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
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.
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.
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.
...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