Forum Moderators: phranque

Message Too Old, No Replies

Weird results from rewrite rule

         

stoveboltgeek

3:33 pm on Jul 8, 2022 (gmt 0)

Top Contributors Of The Month



I'm trying to get this rewrite rule to work, but no matter what I change, it redirects to index.html in the /gallery/ directory.
RewriteCond %{REQUEST_URI} /news/gallery/(.*)$
RewriteRule ^(.*) https://www.example.com/gallery/%1 [L,R=301]

I tested this in htaccess checker and it works, but on the server, the redirection takes me to the index page instead of the page I'm trying to redirect to.

This is the url I'm trying to redirect.
https://www.example.com/news/gallery/benjaminson_jim_1932.htm

The redirection takes me here.
https://www.example.com/gallery/

I have tried the following;
^(.*)... (.*)... (.*)$ ^(.*)$

They all do the same thing on the server - take me to the index page. In the .htacess checker, only the first one works correctly. The second one creates a loop, and the third one duplicates the url like this.
The new url is https://www.example.com/gallery/benjaminson_jim_1932.htmhttps://www.example.com/gallery/benjaminson_jim_1932.htm

The fourth one takes me to index as well.

[edited by: stoveboltgeek at 4:00 pm (utc) on Jul 8, 2022]


[edited by: not2easy at 4:13 pm (utc) on Jul 8, 2022]
[edit reason] Please see TOS [webmasterworld.com] [/edit]

robzilla

3:57 pm on Jul 8, 2022 (gmt 0)

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



This should suffice:
RewriteRule ^news/gallery/(.*)$ /gallery/$1 [L,R=301]

Maybe someone better versed in this can tell you what's wrong with yours.

stoveboltgeek

4:09 pm on Jul 8, 2022 (gmt 0)

Top Contributors Of The Month



This should suffice:
RewriteRule ^news/gallery/(.*)$ /gallery/$1 [L,R=301]
Maybe someone better versed in this can tell you what's wrong with yours.
That also takes me to the index page.

lucy24

4:21 pm on Jul 8, 2022 (gmt 0)

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



Edit: We overlapped.

I think we noted in the other thread that it isn't necessarily a good idea to use an htaccess checking tool to assess a rule which is to go in config.

robzilla homes in on a basic mod_rewrite principle: never put anything in a RewriteCond that can go in the body of the Rule. But for an external* redirect, the target should include the full protocol and hostname to avoid the chance of chained redirects. (For example, if someone has an elderly bookmark in http.)

What seems to be happening here is that the trailing part of the request isn't getting captured. If the value of %1 is "" (empty string) then obviously you'll be redirected to /gallery/

Try the rule-only version and see if that works as intended. And then we can figure out what on earth your server has against %1 capturing from the RewriteCond. You don't have any supplementary Conditions that you've omitted for posting purposes, do you?


* The word “external” doesn’t necessarily mean go to some other site. It simply means that a message is sent out to the visitor telling them to make a whole new request, as opposed to furtively doing stuff inside the server (aka, in the interest of double markedness, an “internal rewrite”).

robzilla

4:24 pm on Jul 8, 2022 (gmt 0)

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



Do you have other rules in place? Is this a straight redirect from /news/gallery/benjaminson_jim_1932.htm to /gallery/ or is there a chain? You can use your browser's Developer Tools to check, or an online tool such as httpstatus.io.

not2easy

4:34 pm on Jul 8, 2022 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



You have the same discussion going on over here: [webmasterworld.com...] and you have received answers but seem to be ignoring the good advice you're getting.

(.*) does not capture the string you need to complete the rewritten URI.
%1 does not capture the string.
Please go back and read through that thread before starting a second thread for the same question.

As lucy24 said
it isn't necessarily a good idea to use an htaccess checking tool to assess a rule which is to go in config.

stoveboltgeek

7:42 pm on Jul 8, 2022 (gmt 0)

Top Contributors Of The Month



@lucy24
I think we noted in the other thread that it isn't necessarily a good idea to use an htaccess checking tool to assess a rule which is to go in config.
I understand that. Right now, all the rules are in a .htaccess file because that's where the hosting support people put them. I would prefer to have them in the conf file but right now, I'm simply trying to get them working.
Try the rule-only version and see if that works as intended. And then we can figure out what on earth your server has against %1 capturing from the RewriteCond. You don't have any supplementary Conditions that you've omitted for posting purposes, do you?
Yes, I have other rules and conditions in the .htaccess file, but it was my understanding that a RewriteCond only applies to the RewriteRule that immediately follows it as long as you use the L flag. Was my understanding wrong? I commented out the RewriteCond and tested the rule that @robzilla suggested. So we're clearly on the same page, I'm going to post the entire contents of the .htaccess file.
# cat /var/www/html/.htaccess
RewriteEngine On
RewriteOptions Inherit
RewriteRule ^links/ /links.html [L,R=301]
RewriteCond %{QUERY_STRING} ubb=get_profile;u=(\d+)\b
RewriteRule ^bboard/.*$ https://www.example.com/ubbthreads/ubbthreads.php?ubb=showprofile&User=%1 [L,R=301]
RewriteCond %{QUERY_STRING} page=get_profile;u=(\d+)\b
RewriteRule ^bboard/.*$ https://www.example.com/ubbthreads/ubbthreads.php?ubb=showprofile&User=%1 [L,R=301]
RewriteCond %{QUERY_STRING} page=editprofile;u=(\d+)\b
RewriteRule ^bboard/.*$ https://www.example.com/ubbthreads/ubbthreads.php?ubb=showprofile&User=%1 [L,R=301]
RewriteCond %{REQUEST_URI} /bboard/cgi-bin/ultimatebb.cgi
RewriteRule (.*)$ https://www.example.com//ubbthreads/ubbthreads.php [L,R=301]
RewriteCond %{QUERY_STRING} page=getprofile;u=(\d+)\b
RewriteRule (.*)$ https://www.example.com//ubbthreads/ubbthreads.php?ubb=showprofile&User=%1 [L,R=301]
#RewriteCond %{REQUEST_URI} /news/gallery/(.*)$
RewriteRule ^news/gallery/(.*)$ https://www.example.com/gallery/%1 [L,R=301]
All of these rules are working as intended, except the last one, which is what we are discussing in this thread.

I just noticed that the rule @robzilla posted used $1 for the reference instead of %1. So, I changed it and tested it again. It still took me to the index page. (I was so hoping that was all it was.) It has to be something subtle that I'm missing.

@robzilla,
Do you have other rules in place? Is this a straight redirect from /news/gallery/benjaminson_jim_1932.htm to /gallery/ or is there a chain? You can use your browser's Developer Tools to check, or an online tool such as httpstatus.io.
Thanks for the tips on Developer Tools and httpstatus.io.

@not2easy,
You have the same discussion going on over here: [webmasterworld.com...] and you have received answers but seem to be ignoring the good advice you're getting.
It was my opinion that this was a new topic separate from the previous thread. I felt if I posted this question in that thread, people would say I was off topic for that thread. I apologize. I'm still learning the ropes here.

I am not ignoring the advice I'm getting here. I'm trying to integrate it. If you look at the rules I've posted here, you will notice that I changed from using * to + based on @lucy24's recommendation. I tried @robzilla's suggestion immediately after he posted it.

I'm also trying to learn how to move these rules over to the conf file, because the good folks in this forum have pointed out repeatedly that .htaccess files should not be used whenever possible.

I'm not trying to be obstreperous. I'm 74 and retired. I do this on a volunteer basis and have to pore over voluminous amounts of documentation to figure things like this out. I only post in the forum after I've done tons of searching and testing and have reached an impasse.

[edited by: not2easy at 8:13 pm (utc) on Jul 8, 2022]
[edit reason] Please Use example.com For Domain Names in Apache Web Server forum [/edit]

lucy24

8:18 pm on Jul 8, 2022 (gmt 0)

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



my understanding that a RewriteCond only applies to the RewriteRule that immediately follows it as long as you use the L flag
A RewriteCond always belongs to its adjoining rule; the [L] flag has no effect on this. So the question was whether you might possibly have something like--putting this as generically as possible--

RewriteCond blahblah string(capture)
RewriteCond other-irrelevant-stuff
RewriteRule pattern target%1 [flags]

because a quirk of captures in the %1 form is that they only work with the most recently matched Condition, even if that was the only one with a capture.

RewriteRule ^news/gallery/(.*)$ https://www.example.com/gallery/%1 [L,R=301]
Oh, yikes, remember that if you’re capturing from the Rule rather than from the Condition, you have to say $1 rather than %1. (I suppose it's too much to hope that the solution turned out to be that simple...)

robzilla

8:23 pm on Jul 8, 2022 (gmt 0)

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



I just noticed that the rule @robzilla posted used $1 for the reference instead of %1. So, I changed it and tested it again. It still took me to the index page. (I was so hoping that was all it was.) It has to be something subtle that I'm missing.

You use % for matches found in the RewriteCond and $ for those in the RewriteRule. So your use of %1 was correct -- as was my use of $1 ;-)

because the good folks in this forum have pointed out repeatedly that .htaccess files should not be used whenever possible.

Strictly speaking, yes, but you're unlikely to notice the difference. It's more of a micro optimization, especially if you have a limited number of rules. And .htaccess is a bit more flexible, of course.

# cat /var/www/html/.htaccess
RewriteEngine On
RewriteOptions Inherit

RewriteOptions Inherit is pointless here, assuming that /var/www/html/ is the root folder of your website. There are no parent directories to inherit .htaccess rules from.

Is there by any chance a separate .htaccess file in /news/ or /news/gallery/? (Or a rule in your Apache conf that affects any of those paths)

[edited by: robzilla at 8:24 pm (utc) on Jul 8, 2022]

stoveboltgeek

8:24 pm on Jul 8, 2022 (gmt 0)

Top Contributors Of The Month



Well, I'm hoist on my own petard. @robzilla's suggested rule works fine. My browser cache was getting in the way. I tested the page in Chrome and worked perfectly. I then dumped Safari's cache, and it worked fine there was well. So, another dragon slain. Another lesson learned. Feel free to close this thread and mark it as solved.

robzilla

8:28 pm on Jul 8, 2022 (gmt 0)

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



Ah! Yes, that pesky browser cache. When working with redirects, it pays to keep your Developer Tools open with "Disable cache" toggled on in the Network tab. That'll prevent any redirects (and any resources) from being loading from the cache.

Glad you got it working now.

stoveboltgeek

9:48 pm on Jul 8, 2022 (gmt 0)

Top Contributors Of The Month



@phranque
RewriteOptions Inherit is pointless here, assuming that /var/www/html/ is the root folder of your website. There are no parent directories to inherit .htaccess rules from.

Is there by any chance a separate .htaccess file in /news/ or /news/gallery/? (Or a rule in your Apache conf that affects any of those paths)

I had already removed inherit after reading over the docs again. The only other .htaccess files on the server are in various directories of roundcubemail denying access to relevant subdirectories.

phranque

10:01 pm on Jul 8, 2022 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



@phranque has entered the discussion...

lucy24

11:39 pm on Jul 8, 2022 (gmt 0)

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



My browser cache was getting in the way.
Another option is to temporarily express all rules as 302 rather than 301, so the browser doesn't try to recycle whatever redirect it received five minutes ago. But, yes, this is the point at which you pore over the browser's keyboard shortcuts and learn the command for Force Reload.

stoveboltgeek

2:23 am on Jul 9, 2022 (gmt 0)

Top Contributors Of The Month



@lucy24
But, yes, this is the point at which you pore over the browser's keyboard shortcuts and learn the command for Force Reload.

My problem is remembering to use it. On my Macbook Pro it's Cmd-R.

robzilla

7:55 am on Jul 9, 2022 (gmt 0)

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



But, yes, this is the point at which you pore over the browser's keyboard shortcuts and learn the command for Force Reload.

How does one Force Reload a redirect, though?

lucy24

3:13 pm on Jul 9, 2022 (gmt 0)

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



True, I don't think that can be done except by clearing the cache. (If anyone wondered, I had to learn Force Reload--I think it's cmd-shift-R, not the cmd-R of a plain reload--because that's the only way to reload all supporting files, as when I’ve edited an image and need to ensure I’ve got it right this time.)

stoveboltgeek

4:40 pm on Jul 9, 2022 (gmt 0)

Top Contributors Of The Month



Thanks, @lucy24 for pointing out my error. I was unaware of cmd-shift-R and wondered why the reloads weren't doing the trick. I learn something new every time one of you posts.

not2easy

5:02 pm on Jul 9, 2022 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



I use a browser with zero cache whenever I'm making changes. In Firefox I just set cache size to "0". Yes, it uses more bandwidth that way but I have no cache confusion and I can use a different browser when caching would be helpful.