logo
Apache Lounge
Webmasters

 

About Forum Index Downloads Search Register Log in RSS X


Keep Server Online

If you find the Apache Lounge, the downloads and overall help useful, please express your satisfaction with a donation.

or

Bitcoin

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.
Post new topic   Forum Index -> Apache View previous topic :: View next topic
Reply to topic   Topic: ServerNames with Trailing Dot
Author
jmweb



Joined: 08 Jun 2017
Posts: 21
Location: USA, Charlotte

PostPosted: Thu 27 Feb '25 6:13    Post subject: ServerNames with Trailing Dot Reply with quote

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

PostPosted: Fri 28 Feb '25 5:43    Post subject: Reply with quote

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


Reply to topic   Topic: ServerNames with Trailing Dot View previous topic :: View next topic
Post new topic   Forum Index -> Apache