Forum Moderators: phranque

Message Too Old, No Replies

Rewriting all children without explicit (.*$)

         

csdude55

9:03 pm on Apr 5, 2020 (gmt 0)

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



I was under the impression that this would rewrite example.com/foo/whatever.php to example.com/bar/whatever.php:

RewriteRule ^foo(/)? /bar$1 [NC,L]

I've been using a sandbox site to test, and it works there:

[htaccess.madewithlove.be?share=0a057272-9059-587a-b535-3c47686314ec...]

but in my live site it doesn't match; I have to change it to:

RewriteRule ^foo(/.*)? /bar$1 [NC,L]

My configuration is:

Server Version: Apache/2.4.41 (cPanel) OpenSSL/1.1.1f mod_bwlimited/1.4 PHP/5.6.40
Server MPM: prefork

Any guess on why it's not matching? Is it something specific with 2.4.41, or maybe something else in my .htaccess? Or is it not supposed to match, and the sandbox is wrong?

lucy24

10:28 pm on Apr 5, 2020 (gmt 0)

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



Frankly I'm surprised it ever matched at all. (In fact, does it really match or is the htaccess tester site badly written?) The append-the-rest-of-the-path behavior is a characteristic of mod_alias redirects and has never been an aspect of mod_rewrite. So yes, you need to do the (/.*) business--though at that point you don't need the / since it would be included in .* anyway.

csdude55

10:51 pm on Apr 5, 2020 (gmt 0)

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



I gotcha... I don't know if the tester has mod_alias installed, so maybe that's why it works? Or it could just be wrong :-/

As for the (/.*)?, my goal was to require a / before accepting anything else; eg, if this is the URI:

example.com/foobarblahblah

I wouldn't want it to match at all. But as it is (with or without the /), it rewrites to:

example.com/barbarblahblah

Any thoughts on "only match the rest of the path if it begins with /"?

csdude55

10:54 pm on Apr 5, 2020 (gmt 0)

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



Wait, hang on... I just realized that I have another RewriteRule in the htaccess that does the same thing, but works!

RewriteRule ^2[0-9]+/ /lorem/

In this one I require the trailing / because it would only ever be called with a file name after, like example.com/20200405/image.jpg. But it works with no problems, so... err?

w3dk

11:38 pm on Apr 5, 2020 (gmt 0)

10+ Year Member Top Contributors Of The Month



As for the (/.*)?, my goal was to require a / before accepting anything else


If it's "required" then why make it optional?

lucy24

12:02 am on Apr 6, 2020 (gmt 0)

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



If it's "required" then why make it optional?
That part makes sense: It means there can be
foo
or
foo/blahblah
but not
fooblahblah.

... except that it wouldn't make any difference, because there's no closing anchor.
fooblahblah
would simply be read as
foo
ignoring the optional-extra bit. The /etcetera would only work as intended if you put a $ closing anchor at the end of the pattern:
^foo(/.*)?$
... which you can't do, because the whole point is that there's supposed to be more stuff after the directory name.

RewriteRule ^2[0-9]+/ /lorem/

In this one I require the trailing / because it would only ever be called with a file name after, like example.com/20200405/image.jpg. But it works with no problems, so... err?
“err” indeed. I want to know more about this. We’re talking htaccess, right? So you don’t have the option of looking at the RewriteLog and seeing what's happening. You're saying that if you request
example.com/2020/blahblah.jpg
you will be served content from
example.com/lorem/blahblah.jpg

:: wandering off to pore over docs to see what on earth I've overlooked ::

csdude55

1:21 am on Apr 6, 2020 (gmt 0)

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



The /etcetera would only work as intended if you put a $ closing anchor at the end of the pattern:
^foo(/.*)?$
... which you can't do, because the whole point is that there's supposed to be more stuff after the directory name.

Yup :-( I'm leaning towards two separate rules: one for if there's a $1 to be sent, another if there's not.

:: wandering off to pore over docs to see what on earth I've overlooked ::

No wait, let me retract that... I'm testing on a /ww2/ subdomain, and didn't realize that the image I was looking at was coming from www. So you were probably right the first time... sorry for the false alarm!