Forum Moderators: phranque

Message Too Old, No Replies

htaccess redirect with a query string?

redirect multiple pages using query strings to one page

         

maestroc

2:46 pm on Jul 24, 2020 (gmt 0)

5+ Year Member



I need to be able to redirect pages with various strings on the end to one specific page. For example pages such as these:

https://example.org/all-episodes-by-category#catid211
https://example.org/all-episodes-by-category#catid128
https://example.org/all-episodes-by-category#catid103
and dozens more

need to all be redirected to:

https://example.org/monthly-archives-of-shows

I know basic 301 redirection but don't know how to do this kind of wildcard redirect. Any help would be greatly appreciated.

w3dk

3:00 pm on Jul 24, 2020 (gmt 0)

10+ Year Member Top Contributors Of The Month



None of the URLs you've posted contain a "query string". The "catid211" part after the "#" (hash/pound symbol) is a fragment identifier.

The "fragment identifier" is not passed to the server as part of the HTTP request, so you cannot perform this type of redirect using Apache/.htaccess.

You need to use JavaScript instead.

lucy24

6:19 pm on Jul 24, 2020 (gmt 0)

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



so you cannot perform this type of redirect using Apache/.htaccess
Well, you can if you want to redirect all fragments, because then you’re just redirecting the URL itself. It only gets impossible* if some fragments go to URL #1 and others go to URL #2.

I would like to think that no browser is dimwitted enough to try to reappend a fragment identifier (the way you could legitimately do with a query string) when going to an entirely new URL.


* Where “impossible” = “has to rely on scripting”. And even then, the script could only operate if the user first goes to the page you’re trying to redirect away from, which seems a waste of time.

w3dk

12:44 am on Jul 25, 2020 (gmt 0)

10+ Year Member Top Contributors Of The Month



Well, you can if you want to redirect all fragments...


Ah yes, of course! (Missed the forest for the trees!)

I would like to think that no browser is dimwitted enough to try to reappend a fragment identifier...


However, the browser DOES reappend the fragment identifier (fragid). I think the only way to remove it fully is to use JavaScript?

You can do something like the following:


Redirect /foo /bar#


Or,


RewriteRule ^foo$ /bar# [NE,R,L]


...to essentially append an empty fragid - this prevents the browser adding back the original fragid, but you are still left with a trailing "#" on the URL. (?)

maestroc

3:39 am on Jul 25, 2020 (gmt 0)

5+ Year Member



So I'm getting confused but tried this anyway. Tried it as this in the htaccess (rewrite is already on):

RewriteRule ^all-episodes-by-category$ /monthly-archives-of-shows [NE,R,L]

But it doesn't do anything. Am I reading your reply incorrectly?

lucy24

3:40 am on Jul 25, 2020 (gmt 0)

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



However, the browser DOES reappend the fragment identifier (fragid).
Oh, ###, does it really? I didn't think to try it out before posting. (Insert “To assume is to make an etcetera” boilerplate here.) Thankfully it's only a problem if the new URL (a) happens to have a fragment with the same id--because if not, the browser just takes you to the top of the page--and (b) that named fragment involves different content than the same-named fragment in the old URL.

:: quick run to test site ::

I'll be darned. At least in Firefox, but I suppose that means all browsers do it. I was going to say: how silly, but on further thought it makes perfect sense.

Consider a canonicalization redirect, where someone asking for
http://www.example.com/pagename.html#midway
could be sent to
https://example.com/pagename.html#midway
and it would be annoying if the fragment didn't get reappended. And the browser doesn't really know if the redirect target is actually the same page by another name.

So, OK, my bad, it is not a dimwitted behavior on the browser's part.

lucy24

3:43 am on Jul 25, 2020 (gmt 0)

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



Separate post, as we overlapped:

RewriteRule ^all-episodes-by-category$ /monthly-archives-of-shows [NE,R,L]
Since there's no # in the target you don't need the [NE] flag. Is the URL exactly
https://example.com/all-episodes-by-category
without extension? Is the rule located in the appropriate section of your htaccess, among other specific redirects?

Also, the redirect target should include the full protocol-plus-domain.

phranque

9:12 am on Jul 25, 2020 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



I would like to think that no browser is dimwitted enough to try to reappend a fragment identifier (the way you could legitimately do with a query string) when going to an entirely new URL.

as far as i know, the handling of document fragment identifiers has never been described in the protocol.
there's only this 20+ yo draft doc:
Handling of fragment identifiers in redirected URLs [tools.ietf.org]

lucy24

4:42 pm on Jul 25, 2020 (gmt 0)

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



See follow-up post, in which I figure out that it is not dimwitted but potentially essential :) The fragment is also reappended in Chrome; between the two of them that accounts for the two main rendering engines.

This suggests that the observation from 1999 would no longer be correct (anyone got an ancient copy of Netscape or MSIE 3 lying around?):
Anecdotal evidence suggests that in fact only about one third of Web browsers re-applies the fragment identifier to the redirected URL.
But the philosophical speculations are the same then as now:
The former assumes that the document may have changed location, but
that it is still the same document and it still contains the same
fragment. The latter assumes that, because the document changed
location, it probably also changed contents, and doesn't have that
fragment anymore.

In any case, the experimentation led me to discover an odd error in my test site's redirect code, leading to targets in the form
https://www.example.com/https://www.example.com/directory/filename.html
(fortunately only on the test site) which merits further investigation.

But we digress. :)

phranque

8:41 pm on Jul 25, 2020 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



My point was that you should be surprised to find consistency among browsers when there is no documented agreement on how they should behave.

lucy24

10:38 pm on Jul 25, 2020 (gmt 0)

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



you should be surprised to find consistency among browsers when there is no documented agreement on how they should behave
I would be surprised to find consistency among browsers when there IS documented agreement.

Still with us, maestroc? I realize that from your end this all looks like a confusing and irrelevant digression, but now a bunch of Forums regulars--starting with me--know something they didn't know before. And that's always useful.

maestroc

12:04 am on Jul 26, 2020 (gmt 0)

5+ Year Member



Honestly I'm lost, lol but thank you for the help. Or at least the education here.

So am I reading this right then that I can't redirect this way?

phranque

12:15 am on Jul 26, 2020 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



So am I reading this right then that I can't redirect this way?

if by "this way" you mean this:
RewriteRule ^all-episodes-by-category$ /monthly-archives-of-shows [NE,R,L]

then you need to be more specific here:
But it doesn't do anything.

what was the request like? what was the expected response? what was the actual response? are there any clues in the web server log files?

also lucy24 asked some relevant questions that haven't been answered.