need to remove www from 2nd level subdomain generically in Apache (using rewrite) - apache

I have the problem but with many subdomains: E.g..
sub1.domain.com and new.domain.com and xsub.domain.com and many many more like this.
How do I remove the www from in front of any of these with one generic rule.
E.g. if someone types www..domain.com or http://www..domain.com to change it to
http://.domain.com
Thanks

You may use the rewrite module to remove the www. when it precedes a sub-domain. In this way, an address like: www.sub1.domain.com would be redirected to sub1.domain.com:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !www.domain.com$ [NC]
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,NC,L]
</IfModule>

Modified tested solution but for http only.
#Allow domain of the form www.domain.com
RewriteCond %{HTTP_HOST} !^www\.([^\..]*)\.([^\..]*)$ [NC]
#Otherwise any other form must be rewritten to remove www
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
#Substitue the complete domain using group %1 in the parentheses of the above condition
RewriteRule ^(.*)$ http://%1/$1 [R=301,NC,L]

This will do the job, and return with http://
RewriteCond %{HTTP_HOST} ^www\.(.*)
RewriteRule ^(.*) http://%1/$1 [R,L]
For https, just add the s, as so:
RewriteCond %{HTTP_HOST} ^www\.(.*)
RewriteRule ^(.*) https://%1/$1 [R,L]
Note that this can be done in httpd.conf as well. Using .htaccess is popular but actually slows down the site, especially if there are nested directories.
You do need:
Options +FollowSymLinks
But you do not need Indexes.

Related

Mod_rewrite on a dynamic rule not working

I have several web sites and using the same mod_rewrite. I like to add two additional conditions to it and based on many of solutions that I have found on here and none seem to be working.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
I have my sites working with the following solution.
domain.com forwards to www.domain.com
I like to add the following two conditions to my conditions above.
Remove trailing slashes. If I have www.domain.com/ it does remove
that trailing slash, unfortunately, for some reason if there are
multiple slashes at the end it doesn't www.domain.com//
Removing index.html from www.domain.com/index.html
Making this dynamic is what may be through this off. If I use the suggestion on domain itself, I have not problems. It is once I add http://www.%{HTTP_HOST}%{REQUEST_URI} where everything goes wonky.
i use this rewrite for redirecting domain.com to www.domain.com. No problems with //
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com$1 [R,L]
#this could work for you (untested)
RewriteCond %{HTTP_HOST} !^www.\$ [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}$1 [R,L]
#redirect /index.html to /
RewriteRule ^/index.html / [R=301,L]

RewriteRule not triggered

I'm trying to change from
http://www.myhost.com/en/team/league-of-legends/3798/destiny
to
http://lol.myhost.com/en/team/league-of-legends/3798/destiny
I tryed different combinaisons for my Apache2 server including the following :
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^/(.*)/team/league-of-legends/(.*)/(.*) http://lol.myhost.com/$1/team/league-of-legends/$2/$3 [R=301,L]
But it seems not to work (i checked in an htaccess tester).
What am I doing wrong please ?
You may try this:
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(?:www\.)?myhost\.com$ [NC]
RewriteCond %{REQUEST_URI} ^/([^/]+)/team/league-of-legends/([^/]+)/([^/]+)/? [NC]
RewriteRule . http://lol.myhost.com/%1/team/league-of-legends/%2/%3 [R=301,L,NC]
Redirects permanently
http://www.myhost.com/en/team/league-of-legends/3798/destiny or
http://myhost.com/en/team/league-of-legends/3798/destiny
To:
http://lol.myhost.com/en/team/league-of-legends/3798/destiny
Strings en, 3798 and destiny are assumed to be variable, while team and league-of-legends are assumed to be fixed.
For silent mapping, remove R=301 from [R=301,L,NC]
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory of www.myhost.com domain:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(myhost\.com)$ [NC]
RewriteRule ^[^/]+/team/league-of-legends/ http://lol.%1%{REQUEST_URI} [R=301,L,NC]
See if this works. Let me know if it doesn't.
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^([^\/]*)/team/league-of-legends/(.*)$ http://lol.myhost.com/$1/team/league-of-legends/$2 [R=301,L]

Rule not redirecting .co.uk to .com

I have been trying to get my site, when a visitor goes to .co.uk to be automatically redirected to .com. As well as if they go to domain.com to be taken to www.domain.com
I have the code below in my httpd.conf. It appears to be working with domain.com to www.domain.com but not domain.co.uk or www.domain.co.uk to www.domain.com
RewriteEngine on
RewriteBase /
RewriteCond %{http_host} ^domain.com [NC,OR]
RewriteCond %{http_host} ^domain.co.uk [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,NC]
If your rewrite rules are in an htaccess file, this should work. If they are in a vhost file (or the Apache httpd.conf itself) try to remove the /.
And try to always be case sensitive (get used to because most of languages are case sensitive it's a good habit to take):
If in a .htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain.com [NC,OR]
RewriteCond %{HTTP_HOST} domain.co.uk [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,NC]
If in a vhost or httpd.conf file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain.com [NC,OR]
RewriteCond %{HTTP_HOST} domain.co.uk [NC]
RewriteRule ^(.*)$ http://www.domain.com$1 [R=301,NC]
And please try to use the RewriteLog directive: it helps you to track down such problems:
# Trace:
# (!) file gets big quickly, remove in prod environments:
RewriteLog "/web/logs/mywebsite.rewrite.log"
RewriteLogLevel 9
RewriteEngine On
Tell me if it works.
Is the name of HTTP_HOST case-sensitive? The RewriteCond documentation always lists it as HTTP_HOST, not http_host. The NC flag won't help with that, since it applies to the string values, not variable names.

mod_rewrite - add www

I'm trying to force www for my site address with .htaccess:
RewriteCond %{HTTP_HOST} ^mysite\.com$ [NC]
RewriteRule ^(.*)$ http://www.mysite.coml/$1 [R=301,L]
If I open mysite.com this working ok - it adds www. It becomes http://www.mysite.com/index.php.
But if I open mysite.com/subpage I redirected to http://www.mysite.com/index.php again, instead of http://www.mysite.com/subpage.
But if I open http://www.mysite.com/subpage I don't get redirect (which is the expected behaviour).
How can I fix this? I would like the redirect all requests to my site to the www subdomain.
From the superb HTML5 Boilerplate .htaccess:
<IfModule mod_rewrite.c>
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</IfModule>
The problem is right here:
RewriteCond %{HTTP_HOST} ^mysite\.com$ [NC]
^ specifies the beginning of the string while $ specifies the end.
In order to make it work you need to have a wildcard select after the .com or .+.
to reiterate what Alex said:
<IfModule mod_rewrite.c>
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</IfModule>
Actually, I have to disagree with Mr. Szanto.
It would appear the problem was not with the:
RewriteCond %{HTTP_HOST} ^mysite\.com$ [NC]
Through some additional research (via Google) I found that this worked great for me:
<IfModule mod_rewrite.c>
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^mysite\.com$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</IfModule>
Mr. Szanto's answer, while well formatted, seems to redirect all requests missing the 'www' to be redirected with the 'www' attached. This was not a desirable outcome with most subdomains. The above code only redirects if there is no 'www' AND no subdomain before the TL domain name.
Hope this helps others.

How to always remove WWW from a url with mod_rewrite?

I'm using the following to try and remove WWW from the url:
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule (.*) http://example.com$1 [R=301]
But for some reason it doesn't work. Any suggestions?
Here’s a more generalized solution:
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [L,R=301]
Try:
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
And without mod_rewrite:
<VirtualHost 10.0.0.1:80>
ServerName www.example.com
Redirect permanent / http://example.com/
</VirtualHost>
Virtual hosts can be used by completing the steps in the following URL: Setting Up A Virtual Host in Apache.
As a minor tweak of Kyle's answer, I'd put a / in the RewriteRule match condition, like
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^/(.*)$ http://example.com/$1 [R=301,L]
Otherwise, you get a double slash as a result.
http://www.example.com/smth -> http://example.com//smth
I would always use 307 (temporary redirect) first because if you get it wrong some browsers cache it permanently. I ended up installing Google Chrome just because I couldn't get my Firefox to forget a bad redirect even when I deleted the whole cache.
Here is a solution if you don't want a hard coded domain name. Don't forget to start the rewrite engine or this won't work!
# Start rewrite engine
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
</IfModule>
# Rewrite "www.example.com -> example.com"
<IfModule mod_rewrite.c>
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
</IfModule>