Forum Moderators: phranque

Message Too Old, No Replies

redirecting multiple versions of the same page to one url

         

Lorel

8:22 pm on Dec 31, 2020 (gmt 0)

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



Hi

I"m redesigning a website that had multiple versions of the same page, i.e., :

example.htm
example.html
examples.htm
examples.html

I know how to set up a redirect from one page to the other but not 3 to another page.

Can someone show me how to redirect the first 3 to examples.html

w3dk

12:57 am on Jan 1, 2021 (gmt 0)

10+ Year Member Top Contributors Of The Month



Not sure how literal your example is, but a "simple" regex that matches the first 3 "pages" eg. "examples?\.html?", would also match the 4th/target URL which would naturally result in a redirect loop. To avoid this you could use a negative lookahead, to exclude the target URL, for example:


RedirectMatch 302 ^/(?!examples\.html)examples?\.html?$ /examples.html


OR, use alternation for the 3 URL variations you want to match, for example:


RedirectMatch 302 ^/example(\.htm|\.html|s\.htm)$ /examples.html


Or just create 3 separate rules?

Aside: If you have many such pages I would also consider implementing this redirect in the application itself (instead of Apache), either by overriding the 404 response or in the page itself if the old URLs still resolve?

[edited by: w3dk at 1:11 am (utc) on Jan 1, 2021]

phranque

1:02 am on Jan 1, 2021 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Can someone show me how to redirect the first 3 to examples.html

you could either use 3 consecutive RewriteRule directives or preferably you could combine the 3 into one using an appropriate regular expression for the RewriteRule Pattern.
see this:
https://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewriterule
I know how to set up a redirect from one page to the other but not 3 to another page.

what have you tried so far?

phranque

1:09 am on Jan 1, 2021 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



RedirectMatch 302

if you are using mod_rewrite anywhere (such as your hostname canonicalization redirects) you should avoid mod_alias (Redirect[Match]) everywhere.
see this for why:
https://httpd.apache.org/docs/current/rewrite/avoid.html#redirect
The use of RewriteRule to perform this task may be appropriate if there are other RewriteRule directives in the same scope. This is because, when there are Redirect and RewriteRule directives in the same scope, the RewriteRule directives will run first, regardless of the order of appearance in the configuration file.

in any case this type of redirect should be Permanent (i.e., a 301 status code response)

w3dk

1:19 am on Jan 1, 2021 (gmt 0)

10+ Year Member Top Contributors Of The Month



if you are using mod_rewrite anywhere...


But if you're not using mod_rewrite in the current context then use mod_alias.

And always test with 302 (temporary) redirects first to avoid potential caching issues.

phranque

1:56 am on Jan 1, 2021 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



if you're not using mod_rewrite in the current context

then by definition you are not doing any:
hostname canonicalization redirects

which could be problematic...

w3dk

11:42 am on Jan 1, 2021 (gmt 0)

10+ Year Member Top Contributors Of The Month



then by definition you are not doing any:
hostname canonicalization redirects


You could be doing this in the main server config or virtualhost (or even in the application itself).

lucy24

6:02 pm on Jan 1, 2021 (gmt 0)

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



If you're canonicalizing somewhere other than htaccess, you can end up with duplicate redirects.

In any case, the patterns for RewriteRule and RedirectMatch are essentially the same, so the whole issue is a bit of a tangent.

Lorel

6:52 pm on Jan 1, 2021 (gmt 0)

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



@phranque re "what have you tried so far?"

I have lots of rewrites for other pages that have been changed (all in the same directory) and I tested them and they work.

I checked the page you referenced but didn't see anything there that applied to this situation.

I can set up each RewriteRule separately, i.e., example.htm to example.html, and then examples.htm to examples.html, etc however I assumed that I could put them all in one rule.

w3dk

7:34 pm on Jan 1, 2021 (gmt 0)

10+ Year Member Top Contributors Of The Month



however I assumed that I could put them all in one rule.


Yes, as I stated above.

If using mod_rewrite then just tweak the syntax, for example:


# Either, using a negative lookahead
RewriteRule ^/?(?!examples\.html)examples?\.html?$ /examples.html [R=301,L]

# OR, using alternation
RewriteRule ^/?example(\.htm|\.html|s\.htm)$ /examples.html [R=301,L]


I only included the "/?" prefix on the regex, as you've not actually stated where you are placing these directives... if in .htaccess then it's not required, or if directly in the server config (or virtualhost) then you can you don't need to make the slash prefix optional.

Or, if you want to be verbose:


RewriteRule ^/?(example\.htm|example\.html|examples\.htm)$ /examples.html [R=301,L]


You could also capture "example" from the requested URL and avoid repetition in the substitution string (depending how literal this example is):


RewriteRule ^/?(example)(\.htm|\.html|s\.htm)$ /$1s.html [R=301,L]


You could then use the same rule for multiple different URLs.

To avoid a double redirect with your canonicalisation redirects (as mentioned above) then place these rules first and include the full absolute canonical URL in the substitution string. For example:


RewriteRule ^/?(?!examples\.html)examples?\.html?$ https://example.com/examples.html [R=301,L]


(Aside: However, if you are implementing HSTS then you'll need to redirect to HTTPS on the same host before canonicalising the hostname. In which case you could use %{HTTP_HOST} in the substitution string instead and canonicalise the hostname later.)

As always, test first with a 302 (temporary) redirect to avoid potential caching issues and only change to 301 (permanent) when all is working as intended.

How many such redirects do you need to implement?

Lorel

6:55 pm on Jan 3, 2021 (gmt 0)

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



@w3dk I used the verbose rule and it worked. Thanks for your very detailed explanations.

phranque

1:56 am on Jan 4, 2021 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



I checked the page you referenced but didn't see anything there that applied to this situation.

but your solution...

tangor

3:39 am on Jan 5, 2021 (gmt 0)

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



Just install the readmymind.js library and let it solve all problems.

(let me know if you googled that)