Keep Server Online
If you find the Apache Lounge, the downloads and overall help useful, please express your satisfaction with a donation.
or
A donation makes a contribution towards the costs, the time and effort that's going in this site and building.
Thank You! Steffen
Your donations will help to keep this site alive and well, and continuing building binaries. Apache Lounge is not sponsored.
| |
|
Topic: ServerNames with Trailing Dot |
|
Author |
|
jmweb
Joined: 08 Jun 2017 Posts: 21 Location: USA, Charlotte
|
Posted: Thu 27 Feb '25 6:13 Post subject: ServerNames with Trailing Dot |
|
|
What is the most efficient way to redirect requests for a hostname with a trailing dot to its dotless counterpart?
I considered creating a separate VirtualHost with the trailing dot variant as the ServerName, but it seems Apache strips the trailing dot automatically.
For example, I attempted the following configuration:
Code: |
<VirtualHost *:80>
ServerName domain.tld.
Redirect rule here
</VirtualHost>
|
However, Apache appears to normalize ServerName, treating both domain.tld. and domain.tld as the same, making this approach ineffective. |
|
Back to top |
|
jmweb
Joined: 08 Jun 2017 Posts: 21 Location: USA, Charlotte
|
Posted: Fri 28 Feb '25 5:43 Post subject: |
|
|
I was unable to offload the rewrite overhead to separate VirtualHosts set for hosts with trailing dots. Here is my solution in the event someone wants to accomplish the same.
Code: |
RewriteEngine on
<If "%{SERVER_PORT} =~ /^(80|443)$/">
RewriteCond %{HTTP_HOST} ^(.+?)\.+$
RewriteRule .? %{REQUEST_SCHEME}://%1%{REQUEST_URI} [R=301,END]
</If>
<Else>
RewriteCond %{HTTP_HOST} ^(.+?)\.+$
RewriteRule .? %{REQUEST_SCHEME}://%1:%{SERVER_PORT}%{REQUEST_URI} [R=301,END]
</Else>
|
|
|
Back to top |
|
|
|
|
|
|