Forum Moderators: phranque

Message Too Old, No Replies

RewriteRule for a subdirectory, without access to said subdirectory

         

Robin_reala

6:23 pm on Aug 6, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi guys, I currently serve all my pages as application/xhtml+xml to compliant browsers (i.e. not IE) using mod_rewrite. I recently discovered though that the stats subdirectory supplied by my host is of course also being served as application/xhtml+xml (I should have realised this) but as it's straight HTML Moz is choking on it. Unfortunately my host blocks write access to this directory which is fair enough, so I started trying to fiddle with my current .htaccess to affect the subdirectory in a different way. Here's my current rewrite rule:


AddType text/html;charset=utf-8 .html
RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} Opera [OR]
RewriteCond %{HTTP_USER_AGENT} Gecko [OR]
RewriteCond %{HTTP_USER_AGENT} Konqueror [OR]
RewriteCond %{HTTP_USER_AGENT} KHTML
RewriteRule \.html$ - [T=application/xhtml+xml;charset=utf-8]

I tried doing this:


AddType text/html;charset=utf-8 .html
RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} Opera [OR]
RewriteCond %{HTTP_USER_AGENT} Gecko [OR]
RewriteCond %{HTTP_USER_AGENT} Konqueror [OR]
RewriteCond %{HTTP_USER_AGENT} KHTML
RewriteRule \.html$ - [T=application/xhtml+xml;charset=utf-8]
RewriteRule /stats/\.html$ - [T=text/html;charset=utf-8]

which didn't work. I'm a bit out of my depth with regexps and mod_rewrite, so can anyone give me a clue as to where to start looking?

Thanks

Birdman

6:47 pm on Aug 6, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think you just need to add a couple chareacters(quantifiers) to your regex. Also, I've never see it done in the manner you are using. I think the last two line should be like this:

RewriteRule ^(.*)\.html$ $1.html [T=application/xhtml+xml;charset=utf-8]
RewriteRule ^(.*)/stats/(.*)\.html$ $1/stats/$2 [T=text/html;charset=utf-8]

After looking around, I did find some exampes using your sytntax, so I stand corrected.

RewriteRule /stats/.+\.html$ - [T=text/html;charset=utf-8]

Robin_reala

7:09 pm on Aug 6, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I tried applying your rules as my site is now serving every HTML file as text/html, which fixes the stats but rather defeats the point of the exercise :)

How were you trying to accomplish the desired result? I don't realy understand what the couple of lines you gave me were meant to do.

Robin_reala

7:28 pm on Aug 6, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



OK, I just saw your edit so I tried that as well. No bones - everything's still transmitted as application/xhtml+xml. Should I be joining the two RewriteRule statements up? I tried inserting an [AND] inbetween them in the hope that the syntax would follow the preceding [OR]s but no luck...

Birdman

7:32 pm on Aug 6, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry about that.

Ignore the first two lines I posted. The last line should get you fixed up.

RewriteRule /stats/.+\.html$ - [T=text/html;charset=utf-8]

If the /stats/ folder is located in the public root of your domain(eg. domain.com/stats/), then you should remove the first slash and add the start anchor(caret).

RewriteRule ^stats/.+\.html$ - [T=text/html;charset=utf-8]

I hope this helps.

Birdman

Birdman

7:42 pm on Aug 6, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ok, the more I look at it I think there is a better way. Although I would think the other way is ok too.

Add another RewriteCond to your original code that says if(not /stats) rewrite to xml.

AddType text/html;charset=utf-8 .html
RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} Opera [OR]
RewriteCond %{HTTP_USER_AGENT} Gecko [OR]
RewriteCond %{HTTP_USER_AGENT} Konqueror [OR]
RewriteCond %{HTTP_USER_AGENT} KHTML
RewriteCond %{REQUEST_URI}!^path/to/stats/
RewriteRule \.html$ - [T=application/xhtml+xml;charset=utf-8]

You will need to adjust the path part(the right side).

Note: Make sure to add the space between the closing bracket(}) and the exclamation(!). The forum software strips them for some reason.

Robin_reala

8:09 pm on Aug 6, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ah, excellent :) Appending


RewriteRule ^stats/.+\.html$ - [T=text/html;charset=utf-8]

to the file works a treat. I did try your idea for rewriting based on whether the request wasn't pointed at the stats directory but interestingly I got a server error for any .html page I tried to pull regardless of it's location. Oh well, at least it's working. Thanks!