Forum Moderators: phranque

Message Too Old, No Replies

Rewrite not detecting directory in match

         

ttremain

9:07 am on Jan 31, 2022 (gmt 0)

10+ Year Member



This has been working for years and suddenly stopped breaking content.

/image/.htaccess

RewriteEngine on
RewriteRule ^a([a-f0-9]{12})\.jpg$ /images.php?content=$1 [L]


This stopped working

I can place it in the document root, and it will actually match the url https://example.com/a123456789012.jpg
AND the url https://example.com/image/a123456789012.jpg

I only want to match the later...

According to the logs:
[perdir /home/XXXXX/public_html/image/] strip per-dir prefix: /home/XXXXX/public_html/image/a123456789012.jpg -> a123456789012.jpg

If I'm reading this correctly, it's stripping the folder out of the URL before applying rules.

I wish to only match ^image/a([a-f0-9]{12})\.jpg$ /images.php?content=$1 [L] and I wish to continue to do so from within the image folder.

I am very confused as to what's changed on me.

Please advise.

[edited by: engine at 9:46 am (utc) on Jan 31, 2022]
[edit reason] Please use example.com [/edit]

lucy24

5:58 pm on Jan 31, 2022 (gmt 0)

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



I can place it in the document root
Well, that’s a better idea anyway :) Especially when you're dealing with mod_rewrite, whose inheritance rules are not as straightforward as other mods.

it will actually match the url https://example.com/a123456789012.jpg
AND the url https://example.com/image/a123456789012.jpg
Ordinarily, you would do this in the root by expressing the pattern as
^images/a([a-f0-9]{12})\.jpg

(The closing anchor does no harm but isn't needed unless you’ve got something very unusual going on.)

According to the logs:
[perdir /home/XXXXX/public_html/image/] strip per-dir prefix: /home/XXXXX/public_html/image/a123456789012.jpg -> a123456789012.jpg
Yup, that’s now mod_rewrite works in a directory context, whether in .htaccess or in a <Directory> section. But this log entry only makes sense if it’s applied to a RewriteRule that is, in fact, located in the /image/ directory. Did you forget to delete/comment-out the relevant part of the directory's htaccess before moving the rule to the root?

I am very confused as to what's changed on me.
Unfortunately, only you are in a position to find out. Since this is about htaccess, I assume you are on shared hosting. Was there a recent Apache upgrade, or some other tweak? The directory-stripping seems particularly weird, if as you say it is applied to directories other than the one containing the htaccess file.

Do any of your existing htaccess files say anything about RewriteOptions? There's a rather staggering range of options in 2.4 as compared to 2.2 and earlier.

phranque

10:11 pm on Jan 31, 2022 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



This has been working for years and suddenly stopped breaking content.

defined "breaking" please.
what response did you get from requesting a so-named jpg file?
But this log entry only makes sense if it’s applied to a RewriteRule that is, in fact, located in the /image/ directory. Did you forget to delete/comment-out the relevant part of the directory's htaccess before moving the rule to the root?

this would be my first question.

ttremain

6:57 am on Feb 1, 2022 (gmt 0)

10+ Year Member



Ordinarily, you would do this in the root by expressing the pattern as
^images/a([a-f0-9]{12})\.jpg


When I add the subdir (images, or image) as you're showing, it no longer matches.. rewrite (or Apache?) is removing that dir from the string before trying matches.

ttremain

7:04 am on Feb 1, 2022 (gmt 0)

10+ Year Member



defined "breaking" please.
what response did you get from requesting a so-named jpg file?


Sorry, I get a 301 redirect to the webroot, because it's no longer matching any rules with the folder in the url (in this case /image/*) unless I leave off the folder in the rule..

RewriteRule ^a([a-f0-9]{12})\.jpg$ /images.php?content=$1 [L]


matches, but:
RewriteRule ^image/a([a-f0-9]{12})\.jpg$ /images.php?content=$1 [L]

no longer does...

lucy24

6:01 pm on Feb 1, 2022 (gmt 0)

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



I get a 301 redirect to the webroot
Wh, wh, wh, wh, what now? Redirects don’t just come out of the clear blue sky. What rule is causing this to happen?

Are there other RewriteRules in the same htaccess? Do those continue to work as intended?

In my personal experience, the only time I’ve met unexpected handling of directories in mod_rewrite is when a rule is inside an envelope such as <FilesMatch>--which may well be why Apache strongly discourages this approach. (I haven’t done this in a while, but did it years ago with image-specific rules.) And, even then, I don’t think the directories are stripped away; instead they are replaced by the whole physical filepath.

And, just to double-check: You don't have any mod_alias redirects (“Redirect” or “RedirectMatch”) do you?

ttremain

6:51 pm on Feb 1, 2022 (gmt 0)

10+ Year Member



Wh, wh, wh, wh, what now? Redirects don’t just come out of the clear blue sky. What rule is causing this to happen?

Are there other RewriteRules in the same htaccess? Do those continue to work as intended?

In my personal experience, the only time I’ve met unexpected handling of directories in mod_rewrite is when a rule is inside an envelope such as <FilesMatch>--which may well be why Apache strongly discourages this approach. (I haven’t done this in a while, but did it years ago with image-specific rules.) And, even then, I don’t think the directories are stripped away; instead they are replaced by the whole physical filepath.

And, just to double-check: You don't have any mod_alias redirects (“Redirect” or “RedirectMatch”) do you?


Yes, it bypasses the rule looking for /image/ECT , and moves on down the list. (and hits a WordPress redirect to index.php)
There are no "<FilesMatch>" in the .htaccess
No "Redirect" anything

I may not have worded it well, but I did pound on it for quite some time before posting, including removing nearly all of .htaccess to test. I believe something has happened in httpd.conf, and I've no idea what commands or directives to look for.

lucy24

10:21 pm on Feb 1, 2022 (gmt 0)

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



and hits a WordPress redirect to index.php
Something is not right here, because WP doesn’t do that. Instead, it internally rewrites everything to index.php

It would have been useful to know at the outset that this is a WP site.

:: wandering off to find moderator who speaks fluent WordPress ::

phranque

10:48 pm on Feb 1, 2022 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



I get a 301 redirect to the webroot, because it's no longer matching any rules with the folder in the url (in this case /image/*)

it bypasses the rule looking for /image/ECT , and moves on down the list. (and hits a WordPress redirect to index.php)


Something is not right here, because WP doesn’t do that. Instead, it internally rewrites everything to index.php

what lucy24 said...

According to the logs:
[perdir /home/XXXXX/public_html/image/] strip per-dir prefix: /home/XXXXX/public_html/image/a123456789012.jpg -> a123456789012.jpg
Yup, that’s now mod_rewrite works in a directory context, whether in .htaccess or in a <Directory> section. But this log entry only makes sense if it’s applied to a RewriteRule that is, in fact, located in the /image/ directory. Did you forget to delete/comment-out the relevant part of the directory's htaccess before moving the rule to the root?

still asking this question myself...

not2easy

12:21 am on Feb 2, 2022 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



The URLs in question are not typical WP URLs. Without more information I couldn't guess their origin. Is the /images/ directory an actual directory or one that is generated by a plugin? Is this site completely WP or inter-stitched with html static pages?