Forum Moderators: phranque

Message Too Old, No Replies

Redirect to lowercase but .html only

         

phpmaven

3:40 pm on Mar 5, 2020 (gmt 0)

10+ Year Member



I have the following rules in my config file and they work fine:

RewriteMap lowercase int:tolower
RewriteCond $1 [A-Z]
RewriteRule ^/?(.*)$ /${lowercase:$1} [R=301,L]

But this also rewrites images css js etc...

I only want to redirect .html files

I've tried a few examples I've seen online but none seem to work.

Thanks,

Mark

lammert

3:48 pm on Mar 5, 2020 (gmt 0)

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



Have you tried replacing the rewrite condition with
RewriteCond $1 [A-Z].*\.html$

This should match all URLs with at least one uppercase, followed by zero or more characters and ending in .html.

phpmaven

4:10 pm on Mar 5, 2020 (gmt 0)

10+ Year Member



That's great! Thank you. I figured it was an easy fix

w3dk

4:16 pm on Mar 5, 2020 (gmt 0)

10+ Year Member Top Contributors Of The Month



Or do it all in the RewriteRule directive and remove the condition altogether?


RewriteRule ^/(.*[A-Z].*\.html)$ /${lowercase:$1} [R=301,L]

lucy24

6:04 pm on Mar 5, 2020 (gmt 0)

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



I think this is the uncommon case where it's less server-intensive to defer the capture to the RewriteCond, because 19 times out of 20 the hypothetical capture will just be thrown away otherwise. If your URLs never contain literal periods, the .* in lammert's version can be replaced with

RewriteRule [A-Z][^.]*\.html

(again, note lack of opening anchor) so the server never has to backtrack “Oh, whoops, I was supposed to leave room for .html at the end”.

RewriteRule ^/?
Is the rule lying loose in the config file (most lilkely a vhost section), or is it in a directory section (including htaccess)? The opening slash isn't optional; depending on where the rule is located, it’s either present or absent. Here it isn't relevant because you don’t need an opening anchor at all, but keep it in mind for other rules.

Edit: No matter what exact form you use, the redirect target needs to begin with the full protocol-plus-hostname:
https://www.example.com/etcetera
Otherwise there is the possibility of duplicate redirects.

phpmaven

6:15 pm on Mar 5, 2020 (gmt 0)

10+ Year Member



Thank you all for your responses