Rewrite rule example.com to www.example.com

 
Post new topic   Reply to topic    Aprelium Forum Index -> URL Rewriting
View previous topic :: View next topic  
Author Message
tfh
-


Joined: 03 May 2020
Posts: 87
Location: Netherlands

PostPosted: Fri Jul 29, 2022 3:54 pm    Post subject: Rewrite rule example.com to www.example.com Reply with quote

What is the correct rewrite to do this? Including for all underlying URL's of course. So just to add the "www." in front of a URL in case it's missing.
_________________
https://www.arnauddeklerk.com
https://www.file-hunter.com
Back to top View user's profile Send private message Visit poster's website
Horizon
-


Joined: 18 Feb 2022
Posts: 25

PostPosted: Fri Jul 29, 2022 8:39 pm    Post subject: Reply with quote

Hello,

You can do this using a 'matches regex' condition for the HTTP_HOST / Host HTTP header with:
Code:
^(?!www\.)(.+)

Above is a positive pattern match with a negative non-capturing group.

(?!anything) means that not only the pattern is not captured to a $d variable, but also that the pattern matching will entirely be invalid if its condition is not met.

And this even if other parts of a regex actually have matches in a text.

Basically if even one group such as (?!anything) fails validation, it is not captured but also all the whole pattern is invalidated even if there were otherwise partial matches elsewhere in the text.

The ^ character ensures that only the beginning is checked.
(?!anything) means must not contain 'anything'.

For the 'redirect to' action, make it an external redirect to:
Code:
www.$1

- Remember not to escape the dot for www in the redirect to, it is not needed.

- You also must have the non-www hostname included in your website entry for Abyss Web Server.

Otherwise you cannot intercept the wrong domain and redirect it to the correct one.

If you don't do it people will directly meet Abyss's 403 landing page or the default hostname if enabled.

- You as well must have a valid SSL certificate for the non-www hostname if you want to be able to redirect people who directly connect to the wrong hostname over HTTPS.

Otherwise they will see a certificate invalidity warning in their web browser.

Make sure to include the non-www hostname in your CSR if it was not generated with Abyss Web Server or if not acquired over ACME by Abyss itself.

Additionally, if you are 100% sure that you do not want to use the non-www hostname you can use a permanent redirect instead of a temporary one.

This way visitors' web browsers will remember the redirect and will directly connect to the correct www hostname next time.

This reduces the HTTP requests per second workload, but not necessarily your server workload itself.

This answer is not just the correct RegEx, but rather also some reminders of things to do as well for it to work.

I think that you are a long-time user, so you probably already know the rest.
It could however be useful for others if they did not know how to do it yet.
Back to top View user's profile Send private message
tfh
-


Joined: 03 May 2020
Posts: 87
Location: Netherlands

PostPosted: Fri Jul 29, 2022 10:30 pm    Post subject: Reply with quote

Thanks for the help! It works great!
_________________
https://www.arnauddeklerk.com
https://www.file-hunter.com
Back to top View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    Aprelium Forum Index -> URL Rewriting All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB phpBB Group