Mobile app version of vmapp.org
Login or Join
Sherry384

: I want to redirect anyHOST.anyTLD to www.anyHOST.anyTLD I'm trying to find a generic enough modrewrite rule that will acomplish following: Redirect anyHost.anyTld to www.anyHost.anyTld Not mess with

@Sherry384

Posted in: #Apache #Htaccess #ModRewrite

I'm trying to find a generic enough modrewrite rule that will acomplish following:


Redirect anyHost.anyTld to anyHost.anyTld Not mess with any other subdomains, for examle other.anyHost.anyTLD should be left alone


Can you help?
Does this slow down server too much?

10.02% popularity Vote Up Vote Down


Login to follow query

More posts by @Sherry384

2 Comments

Sorted by latest first Latest Oldest Best

 

@Welton855

Zach's answer will not work with subdomains like forum.example.com, the next rules checks whether the host equals example.com. If it does, it will be redirected to example.com. No rules will be processed after this one (L = Last).

RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule (.*) www.example.com/ [R=301,L]


This does not cause a server slowdown, but if you really want to get every single performance bit out of it, you could create a virtual host with domain example.com with a redirect rule:

NameVirtualHost 1.2.3.4:80
<VirtualHost 1.2.3.4:80>
ServerName example.com
Redirect / www.example.com/ </VirtualHost>
# other hosts here


Virtual Hosts documentation

10% popularity Vote Up Vote Down


 

@Kaufman445

You should be able to use something like this:

RewriteCond %{HTTP_HOST} !^www.(.*)$

RewriteRule (.*) / [NC]

Let me know how it turns out, I don't have access to an Apache machine to test it right now :)

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme