Forum Moderators: phranque

Message Too Old, No Replies

.htaccess rewriterule not affecting an ajax call

Problem getting my htaccess file do what i want it to in some cases

         

pixeline

11:41 pm on Dec 8, 2008 (gmt 0)

10+ Year Member



Hello!

Here is the issue: i have a few rewriteRule set via .htaccess. It works fine except with one specific ajax call that i do.

Here is the url of the ajax call:


http://localhost/domain.com/dev/medias/03-05-2008_bright-archite/

And here is the Rewrite rule directive:


RewriteRule ^medias/([a-z0-9-]+)/$ index.php?section=news&sortBy=medias&item=$1&dsAnchor=&%{QUERY_STRING} [NC,L]

What should happen is the request should be redirected to my index.php page, yet it does not and displays the directory listing as a result of the ajax call.
Can someone explain why the redirection does not occur?

In case it helps, here is the entire htaccess file content:
Anybody can help? Please i'm really stuck.

In case it helps, here is the full htaccess:


RewriteEngine on
#--------------------------------------------
# PRETTY URLS
#--------------------------------------------
# Externally redirect to add missing trailing slash
RewriteRule ^(([a-z0-9-]+/)*[a-z0-9-]+)$ http://localhost/domain.com/dev/$1/?%{QUERY_STRING} [NC,R,L]
# SEVEN PARAMS
RewriteRule ^([a-z0-9-_]+)/([a-z0-9-_]+)/([a-z0-9-_]+)/([a-z0-9-_]+)/([a-z0-9-_]+)/([a-z0-9-_]+)/([a-z0-9-_]+)/$ index.php?section=$1&sortBy=time&dsAnchor=2007&item=$2&menu=$3&content=$4&id=$5&title=$6&js=$7&%{QUERY_STRING} [NC,L]

# SIX PARAMS
RewriteRule ^([a-z0-9-_]+)/([a-z0-9-_]+)/([a-z0-9-_]+)/([a-z0-9-_]+)/([a-z0-9-_]+)/([a-z0-9-_]+)/$ index.php?section=$1&sortBy=time&dsAnchor=2007&item=$2&menu=$3&content=$4&id=$5&title=$6&%{QUERY_STRING} [NC,L]

# FIVE PARAMS
RewriteRule ^([a-z0-9-_]+)/([a-z0-9-_]+)/([a-z0-9-_]+)/([a-z0-9-_]+)/([a-z0-9-_]+)/$ index.php?section=$1&sortBy=time&dsAnchor=2007&item=$2&menu=$3&content=$4&id=$5&%{QUERY_STRING} [NC,L]

# FOUR PARAMS
RewriteRule ^([a-z0-9-_]+)/([a-z0-9-_]+)/([a-z0-9-_]+)/([a-z0-9-_]+)/$ index.php?section=$1&sortBy=time&dsAnchor=2007&item=$2&menu=$3&content=$4&%{QUERY_STRING} [NC,L]
#
# THREE PARAMS : projects/touch/texts/
RewriteRule ^([a-z0-9-]+)/([a-z0-9-]+)/([a-z0-9-]+)/$ index.php?section=$1&sortBy=time&item=$2&menu=$3&%{QUERY_STRING} [NC,L]
#
# TWO PARAMS : PROJECTS
RewriteRule ^projects/([a-z0-9-]+)/$ index.php?section=projects&item=$1&dsAnchor=&%{QUERY_STRING} [NC,L]

# TWO PARAMS : medias
RewriteRule ^medias/([a-z0-9-]+)/$ index.php?section=news&sortBy=medias&item=$1&dsAnchor=&%{QUERY_STRING} [NC,L]

# TWO PARAMS : NEWS
RewriteRule ^news/([a-z0-9-]+)/$ index.php?section=news&item=$1&dsAnchor=&%{QUERY_STRING} [NC,L]

# TWO PARAMS : people
RewriteRule ^people/([a-z0-9-]+)/$ index.php?section=people&item=$1&dsAnchor=&%{QUERY_STRING} [NC,L]

# ONE PARAM
RewriteRule ^([a-z0-9-]+)/$ index.php?section=$1&%{QUERY_STRING} [NC,L]

# TAG URL : index.php?tag=space%2C+navigable+music#5
RewriteRule ^tag/([a-z0-9-]+)/$ index.php?tag=$1&%{QUERY_STRING} [NC,L]


Thank you,

Alexandre

g1smd

12:50 am on Dec 9, 2008 (gmt 0)

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



In your
[a-z0-9-_]
blocks, do make sure that the second hyphen is the very last item in the list (after the underscore), or is escaped.

pixeline

7:17 am on Dec 9, 2008 (gmt 0)

10+ Year Member



hi g1smd,

thanks for the reply. Yet, i double checked and the second hyphen is the very last item in the list. But do you mean that i should add the underscore to the list of chars ?

pixeline

7:39 am on Dec 9, 2008 (gmt 0)

10+ Year Member



Oh god ! g1smd, you saved me! After double reading your reply, i added the underscore in the pattern, and it worked, at least for that "test" url!
I'm really not good with patterns, can i ask what would be the best complete pattern, so that i don't have an issue with another special character?

What it is now is:

RewriteRule ^medias/([a-z0-9-_]+)/$ index.php?section=news&sortBy=medias&item=$1&dsAnchor=&%{QUERY_STRING} [NC,L]

thank you so much ! With the energy brought by the relief of finding the cause to the problem i'm heading over the php forum to see if i can be of help to others.

g1smd

10:52 am on Dec 9, 2008 (gmt 0)

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



See this bit:
([a-z0-9-_]

The hyphen is NOT last. It needs to be the last item.

i.e. swap places with the underscore.

pixeline

11:19 am on Dec 9, 2008 (gmt 0)

10+ Year Member



ok, got it now, thanks !

jdMorgan

5:08 pm on Dec 9, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Best practice is to always escape a hyphen within an alternate-character [group], regardless of it's position within that group. This prevents unexpected parsing or functional errors on servers where "[0-9-]" is parsed as both "0-9" and as "9-]", which has been known to happen - depending on the regular-expressions library installed in the server's operating system. Some libraries will throw an error if the hyphen is not escaped, and if the Webmaster is lucky... Otherwise, it's a silent problem, and just leads to unexpected pattern matches and the hard-to-identify results of those matches.

We had a very recent thread on just this problem -- within the past ten days or so.

Use "[0-9\-]" to prevent this from happening on any server, present or future. Escaping the hyphen prevents any ambiguity in whether a literal hyphen or a regular-expressions character-range operator is intended.

Jim

pixeline

8:22 pm on Dec 9, 2008 (gmt 0)

10+ Year Member



thank you jdMorgan. You have no idea how much those little tricks do help me !