Forum Moderators: phranque

Message Too Old, No Replies

Why does request uri / match example.com/dir/ ?

         

Mike521

1:48 pm on Aug 28, 2012 (gmt 0)

10+ Year Member



I'm setting my homepage to redirect from https to http. I got it partially working but I don't understand one thing, which is causing it to break other rewrites

Here's what I have:

#https homepage to http
RewriteCond %{SERVER_PORT} =443
RewriteCond %{REQUEST_URI} / [NC]
RewriteRule (.*) http://www.example.com/ [R=301,L]

My problem is with the REQUEST_URI / part. I don't have any wildcards or any sort of regex matching, so I would think the only request that should match this is a request for the homepage. But it also matches things like /dir/ - basically any request that ends in / will match.

So this screws up other rewrites for subdirectories that are accessed through https - the rule runs for them as well.

g1smd

3:10 pm on Aug 28, 2012 (gmt 0)

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



With no anchoring "/" matches any URL that begins or contains a slash.

The (.*) matches any URL too.

What's the [NC] flag for when you haven't specified any characters that might need to be aNyCase?

Mike521

3:23 pm on Aug 28, 2012 (gmt 0)

10+ Year Member



nothing, I just started this rule by copying/pasting another where the NC was useful, but here it's useless, I'll remove it

so for the rewrite condition here, I thought if I put ^/ it would also match anything that ends in a slash, meaning I'd have the same problem? for example wouldn't this:

RewriteCond %{REQUEST_URI} ^/

match all of these:

example.com/
example.com/dir/
example.com/dir/dir/

lucy24

5:51 pm on Aug 28, 2012 (gmt 0)

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



I thought if I put ^/ it would also match anything that ends in a slash

You've got your anchors backward.

^ before all content means "begins with"
$ after all content means "ends with"

^ as the first character in grouping brackets means "is not any of these"
^ anywhere else may or may not result in an error, so literal carets should always be escaped
$ in the target followed by a numeral refers to a capture from the pattern
$ anywhere else, et cetera as above


If the rule applies only to the top-level Index page, you don't need a Condition at all. Put it in the Rule as

^$ in htaccess
^/$ in config

Always put as much information as possible into the Rule itself so the server doesn't have to detour to evaluate conditions with every single request.

Mike521

6:26 pm on Aug 28, 2012 (gmt 0)

10+ Year Member



I completely misunderstood ^ and $. I thought ^/ meant anything before the / is matched, and $ after means anything after the / is matched. I made your adjustments:

RewriteCond %{SERVER_PORT} =443
RewriteRule ^$ http://www.example.com/ [R=301,L]

and it worked perfectly, thanks Lucy!

g1smd

6:38 pm on Aug 28, 2012 (gmt 0)

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



^$
(empty/nothing) can also be expressed as
!.
(not something).