Forum Moderators: phranque

Message Too Old, No Replies

mod rewrite issue

Need help with one step

         

mattmcl1

9:23 pm on Apr 18, 2006 (gmt 0)

10+ Year Member



Hi - I am using mod rewrite to allow a directory structure to redirect to a php page that will dynamically create the website. My goal is to allow up to 5 "levels" of directories to be properly processed.

www.mydomain.com/dir1 --> www.mydomain.com/index.php?numvar=1&var1=dir1

www.mydomain.com/dir1/dir2 --> www.mydomain.com/index.php?numvar=2&var1=dir1&var2=dir2

etc.

Here is the .htaccess code

options +FollowSymlinks
RewriteEngine On
#if the URL does not have a trailing backslash
RewriteCond $1!/$
#Append the trailing backslash
RewriteRule (.+) [mydomain.com...] [L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/$ index.php?numvar=5&var1=$1&var2=$2&var3=$3&var4=$4&var5=$5 [L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/$ index.php?numvar=4&var1=$1&var2=$2&var3=$3&var4=$4 [L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/$ index.php?numvar=3&var1=$1&var2=$2&var3=$3 [L]
RewriteRule ^([^/]+)/([^/]+)/$ index.php?numvar=2&var1=$1&var2=$2 [L]
RewriteRule ^([^/]+)/$ index.php?numvar=1&var1=$1 [L]

The code works fine when I only include the rules for 2,3,4 or 5 directory "depth" URL's (by commenting out the last line) but when I uncomment the last line EVERY time I get numvar=1 and var1=index.php regardless of whether the URL is depth = 1, 2, 3, 4, or 5.

I am sure it is something simple but I just can't figure it out...any help appreciated! Thanks!

jdMorgan

10:04 pm on Apr 18, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Be aware that mod_rewrite code in .htaccess will appear to be recursive. That is, once mod_rewrite does a rewrite or a redirect, it starts over, so that access restrictions in httpd.conf and higher-level .htaccess files can be applied. To do otherwise would open a massive security hole.

Therefore, your first and last rules create a neat little loop. Any of your expected URLs are rewritten to /index.php?var1=xyz&var2=... etc, and mod_rewrite restarts. Since there is no trailing slash, the first rule will add one. Now index.php/?var1=xyz&var2=... neatly matches the last rule, so it is rewritten to /index.php?var1=xyz *again*.

I'd suggest you change the first rule like this:


# if the URL does not have a trailing backslash [b]or contain a period[/b]
RewriteCond $1 !(\.¦/$)
# append a trailing backslash
RewriteRule (.+) http://www.example.com/$1/ [R=301,L]

Alternatively, you could add a RewriteCond to the last rule to exclude index.php from being rewritten.

Note that you will have to change the broken pipe "¦" character to a solid pipe character before use; Posting on this forum modifies that character.

Jim

[edited by: jdMorgan at 3:19 am (utc) on April 19, 2006]

mattmcl1

1:41 am on Apr 19, 2006 (gmt 0)

10+ Year Member



Jim,

Thanks a bunch for your response. Curious about your choice of showing it as a 301 redirect. My intention is to have a series of crawlable page like www.mydomain.com/finance/mortgage/application and www.mydomain.com/entertainment/sports/baseball/orioles which are the pages that I want the Search engines to see. Won't the 301 redirect have the search engines see www.mydomain.com/index.php?numvar=3&var1=finance&var2=mortgage&var3=application

I'll test your code in a few minutes and let you know the result.

Thanks,
Matt

mattmcl1

3:17 am on Apr 19, 2006 (gmt 0)

10+ Year Member



I am not sure what/if I did anything wrong but I get an Internal Server Error when using this code.

Still digging...

jdMorgan

3:21 am on Apr 19, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, sorry -- a missing space between "$1" and "!" -- the 'clean-up code' for this forum deletes spaces preceding "!" unless you use a work-around, which I forgot to do. See correction in previous post.

When you get a server error, check your server error log -- it'll save you a lot of time.

Jim

mattmcl1

3:57 am on Apr 19, 2006 (gmt 0)

10+ Year Member



Thanks for the fix and the recommendation. I was trying to get the Rewrite logging to work and that was proving a challenge. Followed the documentation but it wasn't creating or adding anything to the log after I manually created the file. Guess I can save that troubleshooting for the next time I have mod rerewrite problems.

Thanks again for the help!