Make apache automatically strip off the www.? - apache

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.

Related

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]

What's wrong with this Apache mod_rewrite rule?

I'm attempting to write some redirects for an Apache site, but my mod_rewrite skills are rusty. What's wrong with this rule?
RewriteRule ^/en/(.*)$ /$1 [R=301,L]
I expect it to redirect http://example.com/en/whatevs.html to http://example.com/whatevs.html, but it does not seem to match.
RewriteEngine On
RewriteRule ^en/(.*)$ /$1 [R=301,L]
You were close. It's hard in rewrite to remember when to use the starting / and when not to. I put in the rewriteengine on in case it had slipped your mind to include that.
For example:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/en/.*$
RewriteRule ^en/(.*) /$1 [R=301,L]
Note which has the slash and which does not.
And there's some subtle differences also depending on if your rules are in httpd.conf files or in .htaccess files, but I forget the exact differences.

Proper htaccess 301 to prevent duplicate hostname issues?

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]

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.

How can I correctly catch-all subdomains (and redirect) with .htaccess?

I have wild card subdomains (*.domain.com) set up on my server. I now would like to use .htaccess to redirect all *.domain.com requests to a script main.php on my server. I searched for code that would help accomplish the redirect but I have not been entirely successful. The best working code I have found is:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.+)\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/%1 [QSA,R=301,L]
However, www.subdomain.domain.com is redirected to domain.com/www.subdomain instead of domain.com/subdomain. How can this be fixed in the code? Is there a better way of doing this?
Thanks in advance!
Can you try this rewrite rule:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?(.+)\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/%2 [R=301,L]