How to make %{HTTP_HOST} always return non-www

I have this redirect code and I want it to always return %{HTTP_HOST} as non-www no matter what sub-domain was used.

RewriteEngine On
RewriteRule ^.*$ http://www.mywebsite.com/%{HTTP_HOST} [L,R=301]

So, www.website2.com , website2.com , anything.website2.com will always be redirected to http://www.mywebsite.com/website2.com

Thanks for your time.

Answers


I would use Google, and click the first link. http://dense13.com/blog/2008/02/27/redirecting-non-www-to-www-with-htaccess/

Then you can just change the RewriteCond to match anything, rather than specifically not-www


You could try something like this:

# we are now working on HTTP_REFERER,
# change to appropriate variable according to your needs
#   check if we got at least 2 dots in the URL
RewriteCond %{HTTP_REFERER} \.([^\.]+)\.([A-Z]{2,3})$ [NC]
#   if the condition was met, redirect using only last 2 parts of URL
RewriteRule ^ http://www.mywebsite.com/%1.%2 [R,L]

Let me know if this was helpful.


Have your Rewrite rule like this:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} ^(?:[^.]+\.)(.+)$ [NC]
RewriteRule ^ http://www.mywebsite.com/%1%{REQUEST_URI} [L,R]

Need Your Help

Single page application using grunt and tomcat

angularjs tomcat gruntjs

We are building single page application, using AngularJS and Spring Rest services along with spring boot. For front end build we are using grunt and for server site build maven. But we are not sure...

How to use AutoLayout with UIScrollView

ios objective-c xcode uiscrollview autolayout

I have a problem with the auto layout on an UIScrollView.