Proper htaccess 301 to prevent duplicate hostname issues? - apache

I want to make sure there are no duplicate hostname issues and Google Analytics has suggested there are.
So, I want to make sure my home page is always www.example.com and not example.com or I suppose http://www.example.com
How is this done safely?
I found one example, but as I am not a guru at this sort of thing, figured I needed a second opinion or two:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule .? http://www.example.com%{REQUEST_URI} [R=301,L]
The more explanation the better...

You regex in RewriteCond looks suspect since it makes whole hostname optional. You can have this rule instead:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^ http://www.example.com%{REQUEST_URI} [R=301,L,NE]

Related

How to redirect properly

I´m trying to redirect with rewrite rules in Apache .htaccess.
I want to redirect 360invest.pl to 360investment.pl.
But I want one more address on the server which I do NOT want to be redirected. If somebody tries 360invest.pl/bezrzecze it should not redirect to 360investment.pl.
I used this code:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^360invest.pl/bezrzecze$ [NC]
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [OR,NC]
RewriteCond %{HTTP_HOST} ^360invest.pl$ [NC]
RewriteRule ^ http://www.360investment.pl%{REQUEST_URI} [L,R=301]
This works only if you try www.360invest.pl/bezrzecze. It doesn't work if you try http://360invest.pl/bezrzecze.
In addition if you click any link on the website (www.360invest.pl/bezrzecze) it will redirect you to www.360investment.pl - it should not.
I'm not sure what I'm missing here. Thank you for your help.
What you are doing is really messy. The first condition does not make sense, because "360invest.pl/bezrzecze" is not a domain name. It is a domain name and a path. The following conditions with [OR] flags make no sense to me.
Think about what you want to accomplish. Start with the general case, then list your exceptions:
If someone goes to 360invest.pl you want to redirect all urls to 360investment.pl
unless someone goes to the path bezrzecze
The first part is the rule. The second part is a condition that has to be met
RewriteCond %{REQUEST_URI} !^/bezrzecze
RewriteRule ^ http://www.360investment.pl%{REQUEST_URI} [L,R]

Apache Redirect (using .htaccess)

I'm sort of a novice at using mod_rewrite in .htaccess, please forgive me. I've searched far and wide for an answer but perhaps I'm not looking in the right places, or perhaps .htaccess isn't even the right place to do this kind of thing.
Let's say I own two domains: joebloggs.com and bloggs.com.
When you put joebloggs.com into your browser I'd like it to redirect to https://www.bloggs.com/joe.
However, if you go to joebloggs.com/foobar, I'd like to redirect to https://www.bloggs.com/foo/bar.
note: I know this seems counterproductive, going from a shorter URL to a longer one, but the problem is that marketing materials were already disseminated with the URL joebloggs.com/foobar... :-/
The way I've tried to structure this in the .htaccess file hasn't worked thus far.
<IfModule mod_rewrite.c>
RewriteEngine on
#
# redirect joebloggs.com/foobar to https://www.bloggs.com/foo/bar
RewriteCond %{HTTP_HOST} ^joebloggs\.com\/foobar [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.joebloggs\.com\/foobar [NC]
RewriteRule ^(.*)$ https\:\/\/www\.bloggs\.com\/foo\/bar [L,R=301,NC]
#
# redirect joebloggs.com to https://www.bloggs.com/joe
RewriteCond %{HTTP_HOST} ^joebloggs\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.joebloggs\.com$ [NC]
RewriteRule ^(.*)$ https\:\/\/www\.bloggs.com\/joe [L,R=301,NC]
</IfModule>
When I put joebloggs.com into a browser, it redirects correctly to https://www.bloggs.com/joe - but when I try to go to joebloggs.com/foobar, the URL remains intact and thus goes to a 404 page (because it doesn't exist).
What am I doing wrong? Am I not doing this the optimal way, or not using .htaccess/mod_rewrite as it was intended?
Sorry for being long-winded, I hope I gave enough information. Thanks for any help and time spent on this!
The first set of rules is entirely useless. Update the second set to:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?joebloggs\.com$ [NC]
RewriteRule ^/?$ https://www.bloggs.com/foo [R=301,L,NC,QSA]
RewriteCond %{HTTP_HOST} ^(www\.)?joebloggs\.com$ [NC]
RewriteRule ^foo(.*)$ https://www.bloggs.com/foo/$1 [R=301,L,NC,QSA]

How to implement ssl on everything except a specific and/or a few specific files/folders using .htaccess

I am implementing SSL on everything but would like to exclude a specific page or specific set of pages from using SSL. How can I do this. At the moment I am using the simple syntax:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://mydomain/myfolder/$1 [R,L]
If you could could you explain the steps you are taking in order to do this please so that I can actually understand why and what you have done and I will learn something from this?
Thanks for the help,
John
You can use it like this:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !/(excluded1|excluded2)/ [NC]
RewriteRule ^(.*)$ https://mydomain/myfolder/$1 [R,L]
RewriteCond %{REQUEST_URI} ! is for excluding listed paths in https redirection.

htaccess rewrite based on hostname or domain name

I have two different domains (let's say www.site1.com and www.site2.com) that point to the same hosting server.
I need the two different domain names because I want to use the first one for the italian contents and the second one for the english contents. The contents are the same, unless for the language, but the domains have to be different.
So, I'd like to write a rule that lets me translate from:
www.site1.com to /?lang=it
www.site2.com to /?lang=en
I usually use the same domain name for many different languages rewriting from www.site.com/it/ to /?lang=it (of course, a transparent rewriting - the user doesn't see any different URL).
I'd like to achieve the same using different domains but I can't figure out how... I've been working on it for hours and I can't achieve what I want!
Usually I use this:
RewriteCond %{REQUEST_URI} /([a-z]{2})
RewriteRule ^([a-z]{2})[/]*$ /index.php?lang=$1 [NC,QSA]
I can't get this one work, to use different domains:
RewriteCond %{HTTP_HOST} ^www.site1\.com [NC]
RewriteCond %{REQUEST_URI} !^/index.php?lang=it
RewriteRule ^(.*)$ /index.php?lang=it [NC,QSA]
RewriteCond %{HTTP_HOST} ^www.site2\.com [NC]
RewriteCond %{REQUEST_URI} !^/index.php?lang=en
RewriteRule ^(.*)$ /index.php?lang=en [NC,QSA]
Lawrence Cherone - Thank you, that one works like a charm! Now it works:
RewriteCond %{HTTP_HOST} ^www\.site1\.com [NC]
RewriteRule ^(.*)$ index.php?lang=it [NC,QSA]
RewriteCond %{HTTP_HOST} ^www\.site2\.com [NC]
RewriteRule ^(.*)$ index.php?lang=en [NC,QSA]
Of course I check the www redirect before this rule.
Thank you!!

Make apache automatically strip off the www.?

For various reasons, such as cookies, SEO, and to keep things simple, I would like to make apache automatically redirect any requests for http://www.foobar.com/anything to http://foobar.com/anything. The best I could come up with is a mod_rewrite-based monstrosity, is there some easy simple way to tell it "Redirect all requests for domain ABC to XYZ"?
PS: I found this somewhat related question, but it's for IIS and does the opposite of what I want. Also it's still complex.
It's as easy as:
<VirtualHost 10.0.0.1:80>
ServerName www.example.com
Redirect permanent / http://example.com/
</VirtualHost>
Adapt host names and IPs as needed :)
simpler and easier to copy from site to site:
RewriteCond %{HTTP_HOST} ^www\.(.+)$
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
Pretty simple if you use mod_rewrite, as we all do ;)
This is part of the .htaccess from my live website:
RewriteEngine on
# Catches www.infinite-labs.net and redirects to the
# same page on infinite-labs.net to normalize things.
RewriteCond %{HTTP_HOST} ^www\.infinite-labs\.net$
RewriteRule ^(.*)$ http://infinite-labs.net/$1 [R=301,L]
Use an .htaccess file with some mod_rewrite rules:
RewriteEngine On
RewriteRule ^www.SERVERNAME(.*) http://SERVERNAME$1 [L,QSA]
I'm not sure I got the syntax right with the $1 there, but it's well documented. L sends a location: header to the browser, and QSA means Query String Append.
Since you mentioned using mod_rewrite, I'd suggest a simple rule in your .htaccess - doesn't seem monstrous to me :)
RewriteCond %{HTTP_HOST} ^www\.foobar\.com$ [NC]
RewriteRule ^(.*)$ http://foobar.com/$1 [L,R=301]
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.domain.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [R=301,L]
That should do the trick.