.htaccess forward 'www.w.' to 'www.' - apache

I have a weird issue where Google is indexing several of my site's pages as 'www.w.example.com' which is causing issues with my security certificate.
While I'm looking for a solution, I'd like to redirect this using .htaccess but I can't seem to get it to work.
Currently I have:
RewriteRule ^(.*)www\.w\.(.*)$/$ https://www.$1/$2 [R=301,L]
But it doesn't seem to work..
I have multiple domains for this site so ideally it needs to redirect to the correct domain e.g. https://www.example1.com or https://www.example2.com

It is a bad SEO practice to have the same content available on more than 1 URLs.
You need to decide on the best URL you would like to use and then do a 301 redirect of the others to it.

You need to use a RewriteCond for matching host name:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.w\.(.+) [NC]
RewriteRule ^ http://www.%1%{REQUEST_URI} [R=301,L,NE]
However you might still get SSL cert warning because cert negotiation happens before mod_rewrite rules are invoked.

Related

htaccess www sub domain redirect

My problem
I have a sub domain pointing to a different server. The server has a SSL, thus when a user types in the sub domain: https://my.maindomain.co it works great. Using htaccess I'm trying to redirect all users who type in the following web address: https://www.my.maindomain.co or the http:// equivalent to the correct version (https://my.maindomain.co).
I thought the below would work nicely but it does not:
# rule for removing www on sub domains
RewriteCond %{HTTP_HOST} ^www\.([^.]+\.maindomain\.co)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L]
Anybody help me out with this?
For reference I am placing this code in the htaccess on the maindomain.co server, which is hosted on a different server to my.maindomain.co
You can use this rule for your subdomain on the server where the sub domain is hosted. It should take care of your requirements.
RewriteEngine On
RewriteCond %{HTTPS} !^on [OR]
RewriteCond %{HTTP_HOST} !^my\.example\.co$ [NC]
RewriteRule ^ https://my.example.co%{REQUEST_URI} [R=301,L]
Note you must have a valid ssl certificate for www.my.example.co for this to work if they enter www.my.example.co. Otherwise it will still handle redirecting http://my.example.co to https://my.example.co.
First of all what do you mean by 'it does not work'? It would be helpful, if you could specify what is the exact error you get when you try to load https://www.my.subdomain.co
Apart from that i can think of one more factor, if you are using apache server, u might want to add 'www.my.tutella.co' to ServerAlias under virtual host configuration of my.tutella.co
You don't actually need mod_rewrite to do this.
In the .htaccess of your subdomain (or, if you have access to the main Apache configuration, that's more efficient than .htaccess files) you can use this directive:
Redirect / https://my.example.co/
...this uses mod_alias and is much easier to understand and troubleshoot than the mod_rewrite equivalents.

Making sure my rewrite rule is a 301 re-direct

I have the following re-write rule which directs krmmalik.com to krmmalik.com/me
How do i make sure this rule is a 301 re-direct, and if it isnt one already, how can i turn it into one?
I've tried using the mixing and matching the tips from this site
http://www.webweaver.nu/html-tips/web-redirection.shtml
as well as Google's Support Articles and existing SO questions, but not having much luck. Note the re-write rule in itself so far has been working fine.
I've also added a CNAME for "www" to "krmmalik.com" in my DNS file. Is that good enough, or do i need to add a specific 301 redirect for that as well?
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?krmmalik.com$
RewriteRule ^(/)?$ me [L]
Try the following:
RewriteRule ^/?$ /me [L,R=permanent]
The R=permanent flag instructs a 301 status redirect (and you can use R=301 if you prefer, but I think that "permanent" is more readable).
Putting a forward-slash at the start of the /me target URL will tell Apache to redirect the user to the directory named "me" at the web server's public root directory. So in your case it should redirect the user to krmmalik.com/me (or www.krmmalik.com/me).
Also, you don't need to wrap the match pattern in parentheses, because you don't need to capture the slash for later use. So ^/?$ will do the job fine.

Redirecting all traffic based on Host name

An external server (I'll call it "sub.origin.edu") redirects all traffic to my webpage. I want to take all traffic from this host, and redirect it to a different site (which I'll call "http://foo.target.edu/board/").
My .htaccess file is:
RewriteEngine On
RewriteCond ${HTTP_HOST} sub\.origin\.edu [NC]
RewriteRule ^(.*)$ http://foo.target.edu/board/ [R=302]
This doesn't seem to be working. I've confirmed (using PHP) that the host is indeed sub.origin.edu, and the .htaccess file is in the right directory, but this rule just doesn't come into effect. Any suggestions? Thanks.
(If I remove the RewriteCond, the redirect happens, so I can confirm that everything but the rewrite condition is working.)
Use this:
RewriteEngine On
RewriteCond %{HTTP_HOST} sub\.origin\.edu [NC]
RewriteRule ^(.*)$ http://foo.target.edu/board$1 [R=302]
You used the wrong substition character ($ instead of %)
I found this question while trying to complete a re-derict for specific hostnames.
This link was of great help to understand how RewriteCond and RewriteRule work.
http://httpd.apache.org/docs/2.4/rewrite/intro.html
If sub.origin.edu is doing a 3xx redirect, then the browser will issue a new request to your server using your.server.edu as the host name. So this rule will never match that. If this is the case, there's no easy way to tell where the request was redirected from.
If they're using a CNAME, Femi has the correct answer.

.htaccess: Redirect depending on accessed url

I want to, in my .htaccess, redirect the user to another url depending on what the user accesses.
In this case, http://example.com/awesome.com and http://awesome.com is the same site, and if the user is accessing http://example.com/awesome.com, I want him or her to be redirected to http://awesome.com.
Is this feasible?
Edit: With the help of answers, I came up with this working solution:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^awesome.com$
RewriteRule ^(.*)$ http://awesome.com/$1 [R=301]
you can use mod_rewrite (apache2 module)
this is the .htaccess that i use in order to redirect from my old domain to my new one (while keeping the link strcture e.g www.domain1.com/link/linkb.html becomes www.domain1.gr/link/linkb.html)
RewriteEngine On
RewriteCond %(www\.)?domain1\.com$ [NC]
RewriteRule .* http://www.domain1.gr%{REQUEST_URI} [R=301,L]
google mod_rewrite for more information (syntax etc)
Not entirely sure about .htaccess, but you could just use server code on your 404 page to redirect them appropriately; this way you could collect stats, setup a toolbar, or whatever other actions you might want to take.
.htaccess is about authorization, not redirection. I recommend you look at the redirection support for Apache (or whatever web server you're using), which is a much better fit for this problem and just make sure your .htaccess/authorization is in line with the target.
This rule should do it:
RewriteRule ^awesome\.example(/.*)?$ http://www.awesome.example$1 [R=301,L]
Check the Redirect & RedirectMatch options in apache. For simple cases, like yours it's simplier than a mod_rewrite.
Redirect /awesome.com http://ww.awesome.com
or
Redirect permanent /awesome.com http://ww.awesome.com
Now, if example.com and awesome.com are on the same apache server and same virtualhost you're maybe mising the named bases virtualhost things and you're maybe trying to make something really more complex than a simple named base virtualhost definition.

Can I rewrite from subdomain to folder avoiding redirection?

I want my subdomain to point directly to a folder.
That is if an HTTP request is made to apple.example.com,
I want the response to be the contents of example.com/apple/index.html, but not a 301 error.
Is it possible?
Yes, this is exactly the purpose of tools like ISAPI_REWRITE. My hosting company (orcsweb) uses exactly this technique.
-OIsin
Yes, but you need to make sure that your server is able to support virtual domains. I don't know enough about this, but the mod_rewrite is possible without redirects.
# Internally rewrite <subdomain>.example.com/<URLpath> to example.com/subs/<subdomain/<URLpath>
RewriteCond $1 !^subs/
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com
RewriteRule (.*) /subs/%1/$1 [L]
You can serve content from any folder or you can send a redirect to the client, but AFAIK you cannot do both at the same time - you cannot serve content and somehow change the url in browser at the same time.
If it would be possible, it would most definitely lead to many security exploits.