Forum Moderators: phranque

Message Too Old, No Replies

mod rewrite not working JustWrite

Weird thing with subdirectories.

         

dustinnoe

4:52 pm on May 31, 2007 (gmt 0)

10+ Year Member



I am running apache 1.3.33 and I have the following rewriteRules


#Calendar
RewriteRule ^calendar$ calendar/ [R]
RewriteRule ^calendar/?$ calendar.php #This is the only rule that seems to be used
#day view
RewriteRule ^calendar/today$ calendar/today/ [R]
RewriteRule ^calendar/today/?$ calendar.php?action=day&date=today
RewriteRule ^calendar/([0-9]+)-([0-9]+)-([0-9]+)$ calendar/$1-$2-$3/ [R]
RewriteRule ^calendar/([0-9]+)-([0-9]+)-([0-9]+)/?$ calendar.php?action=day&date=$1-$2-$3
#add event
RewriteRule ^calendar/add-event/(single-dayŚmulti-day)$ calendar/add-event/$1/ [R]
RewriteRule ^calendar/add-event/(single-dayŚmulti-day)/?$ calendar.php?action=add-event-$1

Now my problem is that no matter what I enter after calendar/ it always rewrites to calendar.php

So that...

calendar/today/ -rewrites to> calendar.php
calendar/not_even_a_rule/ -rewrites to> calendar.php
calendar/just/does/not/matter/ -rewrites to> calendar.php

Also the [R] flag just seems to be ignored.

This setup works great under apache 2.0, any idea why it's behaving this way?

g1smd

5:25 pm on May 31, 2007 (gmt 0)

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



Put the more specific rules FIRST.

Add an [L] to each so that you do not create a redirection chain.

I always put a RewriteCond expression in for each one, to pick off exactly when each rule will be run.

I believe that [R] on its own is a 302 redirect. You might want [R=301] instead.

dustinnoe

5:38 pm on May 31, 2007 (gmt 0)

10+ Year Member



It just got even more confusing. I commented out all the rules above and I get the same result. Where else could mod_rewrite possibly be getting rules from.

g1smd

5:41 pm on May 31, 2007 (gmt 0)

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



Clear your cache before you retest. Even then, be aware that some browsers "remember" a redirect long after it has been removed, and even after you have cleared the cache. The redirect is "cached" somewhere else inside the browser.

Driven me nuts to work out what is happening, only to open another browser or try it on another machine and see that it was working all along.

dustinnoe

5:45 pm on May 31, 2007 (gmt 0)

10+ Year Member



OK. I know it is not the browser cache. Where else to redirects get cached?

g1smd

5:48 pm on May 31, 2007 (gmt 0)

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



Somewhere inside the browser; somewhere that isn't the main "page" cache.

Sometimes, it's as if there is a "URL cache" somewhere else inside there too.

dustinnoe

6:08 pm on May 31, 2007 (gmt 0)

10+ Year Member



I don't think it is a cache problem as I have cleared everything I can think of and even tried from a different machine. Here are the modified rules based on the suggestions above.


#Calendar
RewriteRule ^calendar/add-event/(single-day¦multi-day)$ calendar/add-event/$1/ [R]
RewriteRule ^calendar/add-event/(single-day¦multi-day)/?$ calendar.php?action=add-event-$1 [L]

RewriteRule ^calendar/([0-9]+)-([0-9]+)-([0-9]+)$ calendar/$1-$2-$3/ [R]
RewriteRule ^calendar/([0-9]+)-([0-9]+)-([0-9]+)/?$ calendar.php?action=day&date=$1-$2-$3 [L]
RewriteRule ^calendar/today$ calendar/today/ [R]
RewriteRule ^calendar/today/?$ calendar.php?action=day&date=today [L]

RewriteRule ^calendar$ calendar/ [R]
RewriteRule ^calendar/?$ calendar.php [L]

I'm going crazy. This thing is really slowing down my project.

What is the possibility that my home router is caching everything?

jdMorgan

6:41 pm on May 31, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It would be very helpful --both to you and to us-- to indicate the purpose of each rule by adding comments.

Putting on my divining hat, here's what I think you want:


# Calendar Rules
#
# Externally redirect to append trailing slash if missing
RewriteRule ^calendar/add-event/(single-dayŚmulti-day)$ http://www.example.com/calendar/add-event/$1/ [R=301,L]
RewriteRule ^calendar/([0-9]+)-([0-9]+)-([0-9]+)$ http://www.example.com/calendar/$1-$2-$3/ [R=301,L]
RewriteRule ^calendar/today$ http://www.example.com/calendar/today/ [R=301,L]
RewriteRule ^calendar$ http://www.example.com/calendar/ [R=301,L]
#
# Externally redirect to canonical domain if non-canonical domain is requested
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
#
# Internally rewrite calendar/ URLs to calendar script
RewriteRule ^calendar/add-event/(single-dayŚmulti-day)/$ calendar.php?action=add-event-$1 [L]
RewriteRule ^calendar/([0-9]+)-([0-9]+)-([0-9]+)/$ calendar.php?action=day&date=$1-$2-$3 [L]
RewriteRule ^calendar/today/$ calendar.php?action=day&date=today [L]
RewriteRule ^calendar/$ calendar.php [L]

Note that the format for external redirects and internal rewrites differs. Note the changes to the RewriteRule [flags]. Note that the trailing slashes on URL-paths to be internally rewritten are no longer optional -- The external redirect will have taken care of this already.

If you include the domain canonicalization rule, then the rules must be kept in the order shown -- Specific URL redirects, canonical domain redirect, internal rewrites.

Replace the broken pipe "Ś" characters above with solid pipe characters before use; Posting on this forum modifies the pipe characters.

Jim

dustinnoe

6:56 pm on May 31, 2007 (gmt 0)

10+ Year Member



Is there a cache for .htaccess rules? If so, how do I disable it?

jdMorgan

8:19 pm on May 31, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No, there are browser caches, ISP caching proxies, and network caches.

Flush your browser cache, and then use CTL-F5 to force page reloading through all intermediary caches.

Jim

dustinnoe

8:57 pm on May 31, 2007 (gmt 0)

10+ Year Member



You said the magic word, "ISP"

It has got to be them. They always seem to do things that annoy me from the development standpoint.