Rule not redirecting .co.uk to .com - apache

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.

Related

.htaccess redirect to another folder but keep the original domain

First,
my 'example.com' domain is linked to '/home/defaultfolder/' folder.
I want to redirect
example.com , www.example.com
to
/home/somefolder/example/ , not /home/defaultfolder/ folder
by using .htaccess. (both are inside the DocumentRoot directory)
also, subdomains like
a.example.com
to
/somefolder/example/a/
but keeps the 'example.com' domain.
I have tried some examples on the web, but nothing could have done it.
How can I write the .htaccess to do so? Thank you.
Of course, I cannot change server settings(like alias virtual hosts..) and that's why I am trying to do it by modifying the file.
I have tried
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteRule /somefolder/example/$1 [R=301,NC,L]
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteRule !^/somefolder/example/ /somefolder/example/1%{REQUEST_URI} [L]
RewriteCond %{HTTP_HOST} ^/?(?:www\.)?example.com
RewriteRule ^(.*)$ /somefolder/example/$1 [R=permanent,L]
and some others..
Have it this way in /home/.htaccess (site root):
RewriteEngine On
# handle example.com
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} ^(?:www\.)?(example)\.com$ [NC]
RewriteRule .* somefolder/%1/$0 [L]
# handle any sub.example.com
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} ^([^.]+)\.(example)\.com$ [NC]
RewriteRule .* somefolder/%2/%1/$0 [L]
I think you should modify the server setting, make the domain root folder to what you want.

generic non-www to www, and non-http to https

I have the following code for my .htaccess file that I've picked up from here and tried adapting it as I understand from .htaccess, yet I can't seem to get it to work (or maybe the browser has cached it but I can't seem to clear it).
Options -Indexes
Options +FollowSymlinks
<IfModule mod_rewrite.c>
########## FORCE SSL ##########
RewriteEngine On
RewriteBase /
# Non-secure requests to www.domain.com should redirect to https://www.domain.com
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www\.%{HTTP_HOST} [NC]
RewriteRule ^(.*)$ https://www\.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# All secure (HTTPS) traffic should redirect to https://www.domain.com
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} !^www\.%{HTTP_HOST} [NC]
RewriteRule ^(.*)$ https://www\.%{HTTP_HOST}/$1 [L,R=301]
</IfModule>
I want to make it as generic a possible so I can simply copy and paste it to any site I make so no need to edit it each time - I guess I could also do this in PHP but I think it would be good if .htaccess is also there.
Another point is, can .htaccess be read if i were to go to www.domain.com/.htaccess or do I need to cover that in a 'deny all' kind of thing?
The second argument to the RewriteCond must be a regex, so it cannot contain a variable.
Try adding the following to your .htaccess file in place of the rules you had
#capture top level domain (.com or .co.uk)
RewriteCond %{HTTP_HOST} ([-_a-zA-Z0-9]+\.([a-zA-Z]{2,5}|co\.uk))$ [NC]
RewriteCond %{HTTP_HOST} (www\.)?(.+)$ [NC]
RewriteRule ^ - [E=MY_TLD:%2]
# Non-secure requests to www.domain.com should redirect to https://www.domain.com
RewriteCond %{HTTPS} off
RewriteRule ^ https://www\.%{ENV:MY_TLD}%{REQUEST_URI} [L,R=301]
# All secure (HTTPS) traffic should redirect to https://www.domain.com
RewriteCond %{HTTPS} on
#if host does not start with www
RewriteCond %{HTTP_HOST} !^www\.[-_a-zA-Z0-9]+\.([a-zA-Z]{2,5}|co\.uk)$ [NC]
RewriteRule ^ https://www\.%{ENV:MY_TLD}%{REQUEST_URI} [L,R=301]
Shortest version would be:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} (www\.)?(.+)$ [NC]
RewriteRule ^ https://www\.%2%{REQUEST_URI} [L,R=301]
Only (somewhat) downside would be that subdomain.example.com gets redirected to www.subdomain.example.com

mod_rewrite with two servers and three subdomains?

I've been looking around (both here and the googles), and I can't find an answer that actually works for me. Basically, I have two physical servers (a dev server and a production server), and I want to be able to use one .htaccess file in my git repo.
Normally this wouldn't be difficult, but I have two domains, example.com and longexample.com. I set up longexample.com and dev.longexample.com's CNAMEs to example.com (and dev.example.com) at my DNS host, and set the apache ServerName/ServerAlias on both servers:
dev server:
ServerName dev.example.com
ServerAlias dev.longexample.com
prod server:
ServerName example.com
ServerAlias longexample.com
ServerAlias www.example.com
ServerAlias www.longexample.com
Here's my .htaccess file:
Options +FollowSymlinks +Indexes
RewriteEngine on
# RewriteCond %{HTTP_HOST} ^www.longexample.com$ [NC,OR]
# RewriteCond %{HTTP_HOST} ^longexample.com$ [NC,OR]
# RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
# RewriteRule ^(.*)$ http://example.com/$1 [R,N]
RewriteCond %{HTTP_HOST} ^dev\.longexample\.com$ [NC]
RewriteRule ^(.*)$ http://dev.example.com/$1 [R,L]
RewriteRule ^([^\.]+)$ $1.html [NC,L]
RewriteCond %{THE_REQUEST} ^GET\ ([^\.]+)\.(html|htm) [NC]
RewriteRule ^([^\.]+)\.(html|htm)$ %1 [R,NC,L]
Supposedly the first two blocks should rewrite prod and dev to the proper urls (I'll make them 301s when everything works), but it doesn't seem to be working. Like it is now, it does nothing. If I set the RewriteRule to [N] instead of [L], it gives me an infinite loop.
The first block is commented out because if I get it working on the dev server, the solution for prod should be readily apparent. The last block makes a request to example.com/about.html redirect to example.com/about (while still using the /about.html file. That part works fine.
Is there some issue with using two domains? I've also tried using %{SERVER_NAME} instead of %{HTTP_HOST}, but nothing changes.
Aha, figured it out! It seems having a chain of [OR]'s messes it up. Here's what works:
Options +FollowSymlinks +Indexes
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.yoshokatana\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^yoshokatana\.com$ [NC]
RewriteRule ^(.*)$ http://yosho.me/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.yosho\.me$ [NC]
RewriteRule ^(.*)$ http://yosho.me/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^dev\.yoshokatana\.com$ [NC]
RewriteRule ^(.*)$ http://dev.yosho.me/$1 [R,L]
RewriteRule ^([^\.]+)$ $1.html [NC,L]
RewriteCond %{THE_REQUEST} ^GET\ ([^\.]+)\.(html|htm) [NC]
RewriteRule ^([^\.]+)\.(html|htm)$ %1 [R=301,NC,L]

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

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.

How can I use mod_rewrite to 301 redirect example.com to www.example.com?

I need to redirect any URLs without "www." to URLs with "www." for better search engine optimization. I read that this is possible with mod_rewrite and .htaccess files, but I do not know the right code to use. Can anyone help?
Create a file called .htaccess in your root folder (the one where, say, index.html or index.php resides). Put the following into it:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^yourdomain\.com [NC]
RewriteRule ^(.*)$ http://www.yourdomain.com/$1 [L,R=301]
There is an excellent example of this in Apache's URL Rewriting Guide.
The following code would redirect any non-www request to a www request:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/(.*) http://www.example.com/$1 [L,R=301]
You'd want to put this inside the <Directory> directive of your .htaccess file, or apache config file.
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule (.*) http://www.example.com/$1 [L,R=301]
To remove www from your url website use this code on .htaccess
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
To force www in your website url use this code on .htaccess
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^YourSite.com$
RewriteRule ^(.*)$ http://www.yourSite.com/$1 [R=301]
RewriteCond %{REQUEST_fileNAME} !-d
RewriteCond %{REQUEST_fileNAME} !-f
RewriteRule ^(([^/]+/)*[^./]+)$ /$1.html [R=301,L]
Were "YourSite.com" you must replace for your url.
In addition to using mob_rewrite you can do this with a virtual host directive
<VirtualHost example.com>
ServerName example.com
Redirect permanent / http://www.example.com
</VirtualHost>
I usually do it the other way around to remove the extraneous 'www'.