Forum Moderators: phranque

Message Too Old, No Replies

Mod Rewrite assistance required

         

UKJay

1:07 pm on Apr 4, 2006 (gmt 0)

10+ Year Member



Afternoon all! Had a good look around but I find this .htaccess stuff all a little confusing at present! So I hope you bear with me on this - I'm doing the best I can :)

What I am trying to do:

My website has a number of templates (or styles) set up and one of them, 'styleid=7', is specifically for PDAs.

In my current root directory .htaccess file:

RewriteEngine On

RewriteCond %{HTTP_USER_AGENT} "Windows CE" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "Blackberry" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "NetFront" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "Palm OS" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "Blazer" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "Elaine" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "^WAP.*$" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "Plucker" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "AvantGo" [NC]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*) /forums/$1?styleid=7 [L,R]

This should work by redirecting any PDA request to any file in the root directory only through immediately to the 'forums/' directory and add '?styleid=7'. (At least thats what I think it does - it works anyhow)

What I am now needing:

I now want to be able to check any filerequests in the /forums/ directory and sub directories and check if the 'styleid=7' rule needs to be applied.

What I have tried to code:

RewriteCond %{HTTP_USER_AGENT} "Windows CE" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "Blackberry" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "NetFront" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "Palm OS" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "Blazer" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "Elaine" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "^WAP.*$" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "Plucker" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "AvantGo" [NC]
RewriteCond %{QUERY_STRING} "!.gif"=[NC]
RewriteCond %{QUERY_STRING} "!vbseo.php" [NC]
RewriteCond %{QUERY_STRING} "!vbseo_getsitemap.php" [NC]
RewriteCond %{QUERY_STRING} "!styleid=7" [NC]
RewriteRule ^(.*)$ $1?styleid=7 [L,R]

I have tried this in the .htaccess in my forums directory but its not working. I have messed about a load and occasionally I would get a 'close hit' where the code did something but not exactly what I needed. More often than not I ended up in a recursive loop.

Can someone help me to achieve what I need overall, namely:

1. Check if browser accessing root folder is PDA and if so, redirect immediately to forums/index.php?styleid=7
2. Check if any files being requested from the forums directory by PDA and sub directory have anything other than styleid=7 applied and if so, delete other style and apply the styleid=7 instead.

Hope this makes sense!

Oh, also, I need to exclude a number of filetypes (eg .gif images, vbseo.php)

An example of how my site URLs end is forums/frequently-asked-questions-faqs/35586-thread-viewing-options.html#post428050

(Admins, please note his is not a full URL, just an example of how my URLs are constructed)

Thanks in advance

Thanks very much.
__________________
Regards

Jay

jdMorgan

3:21 pm on Apr 4, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Jay,

Welcome to WebmasterWorld!

This isn't so much a question as a project! I'll try to point you in the right direction:

There's no need to use an external redirect. I suggest a server-internal rewrite only:


RewriteRule ^(.*)$ $1?styleid=7 [b][L][/b]

More often than not I ended up in a recursive loop.

You need to explicitly prevent this loop in your second code section, just as you did in the first section. Use


RewriteCond %{QUERY_STRING} !styleid=7[^0-9]*

Can someone help me to achieve what I need overall, namely:

1. Check if browser accessing root folder is PDA and if so, redirect immediately to forums/index.php?styleid=7

You've done that in the first code section, I think.

2. Check if any files being requested from the forums directory by PDA and sub directory have anything other than styleid=7 applied and if so, delete other style and apply the styleid=7 instead.

Add something like this:

# Case: Name/value pair precedes styleid
RewriteCond %{QUERY_STRING} !styleid=7[^0-9]*
RewriteCond %{QUERY_STRING} ^(.+)&styleid=[0-9]+(.*)$
RewriteRule ^(forums/.*) /$1?%1&styleid=7%2 [L]
#
# Case: No name/value pair precedes styleid
RewriteCond %{QUERY_STRING}!styleid=7[^0-9]*
RewriteCond %{QUERY_STRING} ^styleid=[0-9]+(.*)$
RewriteRule ^(forums/.*) /$1?styleid=7%1 [L]

Oh, also, I need to exclude a number of filetypes (eg .gif images, vbseo.php)

Add a RewriteCond:

RewriteCond %{REQUEST_URI} !\.(gif¦php)$

Replace the broken pipe "¦" character above with a solid pipe before use; Posting on this board modifies pipe characters.

For more information, see the documents cited in our forum charter [webmasterworld.com] and the tutorials in the Apache forum section of the WebmasterWorld library [webmasterworld.com].

Jim

UKJay

3:36 pm on Apr 4, 2006 (gmt 0)

10+ Year Member



Thanks - I'll set up a test server tonight and have a good play with these to make sure they are working. Thanks once again for your response.

UKJay

9:10 pm on Apr 4, 2006 (gmt 0)

10+ Year Member



Is this my whole .htaccess in the root directory then? Will all of this only be triggered if the PDA (or similar) device is involved and allow ordinary HTTP headed browsers to pass unchanged and am I right to repeat the conditions for each rewrite?

Thanks again for the help - I feel I'm getting closer but its still voodoo :)

RewriteEngine On
#
RewriteCond %{HTTP_USER_AGENT} "Windows CE" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "Blackberry" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "NetFront" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "Palm OS" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "Blazer" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "Elaine" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "^WAP.*$" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "Plucker" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "AvantGo" [NC]
RewriteCond %{REQUEST_URI}!\.(gif¦php)$
RewriteRule ^(.*)$ $1?styleid=7 [L]
#
RewriteCond %{HTTP_USER_AGENT} "Windows CE" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "Blackberry" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "NetFront" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "Palm OS" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "Blazer" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "Elaine" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "^WAP.*$" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "Plucker" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "AvantGo" [NC]
RewriteCond %{REQUEST_URI}!\.(gif¦php)$
RewriteCond %{QUERY_STRING}!styleid=7[^0-9]*
RewriteCond %{QUERY_STRING} ^(.+)&styleid=[0-9]+(.*)$
RewriteRule ^(forums/.*) /$1?%1&styleid=7%2 [L]
#
RewriteCond %{HTTP_USER_AGENT} "Windows CE" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "Blackberry" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "NetFront" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "Palm OS" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "Blazer" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "Elaine" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "^WAP.*$" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "Plucker" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "AvantGo" [NC]
RewriteCond %{REQUEST_URI}!\.(gif¦php)$
RewriteCond %{QUERY_STRING}!styleid=7[^0-9]*
RewriteCond %{QUERY_STRING} ^styleid=[0-9]+(.*)$
RewriteRule ^(forums/.*) /$1?styleid=7%1 [L]

jdMorgan

3:38 am on Apr 5, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes (if the clients identify themselves properly) and yes to the repeat, but I'd suggest you combine those user-agent lines and use the 'skip' function of mod_rewrite for the sake of efficiency:

RewriteEngine on
#
# If mobile device UA or gif or PHP file
RewriteCond %{HTTP_USER_AGENT} !(Windows\ CE¦Blackberry¦NetFront¦Palm\ OS¦Blazer¦Elaine¦^WAP¦Plucker¦AvantGo) [NC,OR]
RewriteCond %{REQUEST_URI} \.(gif¦php)$
# skip next three rules
RewriteRule .* - [S=3]
#
# Rewrite mobile client requests for root directory only to forums subdirectory
RewriteRule ^([^/]*)$ /forums/?styleid=7 [L]
#
# Force style id 7 for mobile clients, case one
RewriteCond %{QUERY_STRING} !styleid=7[^0-9]*
RewriteCond %{QUERY_STRING} ^(.+)&styleid=[0-9]+(.*)$
RewriteRule ^(forums/.*) /$1?%1&styleid=7%2 [L]
#
# Force style id 7 for mobile clients, case two
RewriteCond %{QUERY_STRING} !styleid=7[^0-9]*
RewriteCond %{QUERY_STRING} ^styleid=[0-9]+(.*)$
RewriteRule ^(forums/.*) /$1?styleid=7%1 [L]
#
# Skip directive in the first rule lands here. Place additional rules
# (if any) for .gif files, .php files, and non-mobile user-agents here.
#

Change all broken pipe "¦" characters above to solid pipes before use.

Jim

UKJay

9:57 am on Apr 5, 2006 (gmt 0)

10+ Year Member



Superb Jim - thankyou very much