How can I set Tomcat to automatically redirect to "www"? I want that if a user enters my domain like:
mydomain.com
he will be redirected to: www.mydomain.com
The most common subdomain is www, which stands for World Wide Web. This subdomain contains a website's homepage and its most important pages.
A subdomain is an additional part to your main domain name. Subdomains are created to organize and navigate to different sections of your website. You can create multiple subdomains or child domains on your main domain.
In the Google Cloud console, go to the Custom Domains tab of the App Engine Settings page. Click Add a custom domain. If your domain is already verified, the domain appears in the Select the domain you want to use section. Select the domain from the drop-down menu and click Continue.
The tuckey url rewrite filter can be used like this to do the proper redirection:
<rule>
<name>Canonical Hostnames</name>
<condition name="host" operator="notequal">^www.mydomain.com</condition>
<condition name="host" operator="notequal">^$</condition>
<from>^/(.*)</from>
<to type="redirect" last="true">http://www.mydomain.com/$1</to>
</rule>
If you are using Apache, simple do (on htaccess):
RewriteEngine On
RewriteCond %{HTTP_HOST} ^yourdomain.com
RewriteRule ^(.*)$ http://www.yourdomain.com/$1 [R=301]
This way you make sure anything that is not using www starts using
UPDATE As you mentioned you don't have apache, I remembered I used this about a year ago. It does pretty much the same as mod_rewrite, and is fully supported by Tomcat. I used it with resin though, but I know it works the same way.
Greatest thing about it, is that it also runs on "mod_rewrite style", as you can see here. The only reason why I didn't continue using it, is because it will end up doing it at a server level, as opposed to a webserver level. Meaning it will call the JVM to interpret the redirect.
It works the same way though, and as mentioned before, can sue exactly the same thing you'd use on Apache.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With