Multi-site force www. on ALL subdomains htaccess snippet - .htaccess

I previously got some great help here with a tad bit complicated .htaccess file.
It is a multi-site/domain file and uses %{HTTP_HOST} to rewrite/301 the non-wwww to the www.widgets.com address.
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
The problem is that RegexFu only looks for the NOT www. and then adds the www.
This could cause some duplicate content issues if someone links to say, mail.widgets.com as this is redirected to www.mail.widgets.com.
To summarize:
I need:
mail.widgets.com -> www.widgets.com
dogs.widgets.com -> www.widgets.com
www.mail.widgets.com -> www.widgets.com
www.dogs.widgets.com -> www.widgets.com
etc.
I have tried a variety of permutations liek this with no luck:
RewriteCond %{HTTP_HOST} !^www\.[^\.]+\.com [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
No dice. I either end up with a loop or some other problem.
I need simply anything that isn't:
www. (anything but period) .com - > www. (last string before .com) .com
and of coarse the standard:
(anything but period).com -> www. (anything but period).com
If someone could lend me a hand I would greatly appreciate it.

Try:
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.[^.]+\.[^.]+\.[^.]+ [NC]
RewriteCond %{HTTP_HOST} ([^.]+)\.com$ [NC]
RewriteRule ^(.*)$ http://www.%1.com/$1 [R=301,L]
The %1 backreferences the grouping that matches ([^.]+) in the previous rewrite condition. It ignores any subdomain that's before the domain name, if there is one.

Related

htaccess redirect (multidomain multilanguage) subfolder wildcard

I have a typo3 installation with multidomain and multilanguage, where not every language is setup to every domain.
Languages are de/fr/en/pt/es/cn/
www.example.de can be de/en/fr but not
www.example.de pt/es/cn
now I have messed up sth. and google has indexed loads of wrong urls e.g.
www.example.de/pt/
www.example.de/es/
www.example.de/cn/
they point to languages that are not set for this domain
I am fiddling around in the htaccess to redirect 301 the wrong urls with wildcards(?) to the .tld
I am looking for a solution redirect eg.
www.example.de/pt/* to www.example.de/
www.example.de/es/* to www.example.de/
www.example.de/cn/* to www.example.de/
where the * should represent the complete sting/path following the language parameter.
and of cause the same procedure for the .com domain
www.example.com/fr/* to www.example.com/
www.example.com/de/* to www.example.com/
I searched the web up and down but nothing I tried works.
any help would highly apreciated.
a tiny step further
RewriteCond %{HTTP_HOST} ^www.example.de
RewriteRule ^cn/(.*)$ http://www.example.de/ [L,R=301]
RewriteRule ^pt/(.*)$ http://www.example.de/ [L,R=301]
RewriteRule ^es/(.*)$ http://www.example.de/ [L,R=301]
this seems to work
and now for the second domain as I need to differentiate between .com and .de
RewriteCond %{HTTP_HOST} ^www.example.com
RewriteRule ^de/(.*)$ http://www.example.com/ [L,R=301]
RewriteRule ^fr/(.*)$ http://www.example.com/ [L,R=301]
this now breaks www.example.de/fr/whatever and redirects it to www.example.com as well.
so it looks like the first condition is matching and the the last rule is applied for french.
how can I limit the rules assigning them only to the appropriate domain conditions?
ok looks like every condition and respective rule needs to be written in a single line an repeated
RewriteCond %{HTTP_HOST} ^www.example.de
RewriteRule ^cn/(.*)$ http://www.example.de/ [L,R=301]
RewriteCond %{HTTP_HOST} ^www.example.de
RewriteRule ^pt/(.*)$ http://www.example.de/ [L,R=301]
RewriteCond %{HTTP_HOST} ^www.example.de
RewriteRule ^es/(.*)$ http://www.example.de/ [L,R=301]
RewriteCond %{HTTP_HOST} ^www.example.com
RewriteRule ^de/(.*)$ http://www.example.com/ [L,R=301]
RewriteCond %{HTTP_HOST} ^www.example.com
RewriteRule ^fr/(.*)$ http://www.example.com/ [L,R=301]
this seems to work.
any better solution is highly appreciated

.htaccess wildcard redirect from parked domain?

I have two domains pointing to my site, for example; http://some-long-domain.co.uk and http://short.co - I want to be able to share URLs like http://short.co/x/ic5rai, while redirecting all traffic to the www. version of the main domain, and keeping the path on the URL.
To complicate matters, I don't want to touch versions of the domain with a subdomain that I use for development (local. and staging.).
E.g.;
http://short.co/x/ic5rai -> http://www.some-long-domain.co.uk/x/ic5rai
http://www.short.co/x/ic5rai -> http://www.some-long-domain.co.uk/x/ic5rai
http://some-long-domain.co.uk/x/ic5rai -> http://www.some-long-domain.co.uk/x/ic5rai
http://local.some-long-domain.co.uk/x/ic5rai -> http://local.some-long-domain.co.uk/x/ic5rai
http://staging.some-long-domain.co.uk/x/ic5rai -> http://staging.some-long-domain.co.uk/x/ic5rai
Current re-write rules look like the following, and work for the latter point (local/staging), but not for anything else. What am I doing wrong?
RewriteCond %{HTTP_HOST} !^www.some-long-domain.co.uk$ [NC]
RewriteCond %{HTTP_HOST} !^local.some-long-domain.co.uk$ [NC]
RewriteCond %{HTTP_HOST} !^staging.some-long-domain.co.uk$ [NC]
RewriteCond %{HTTP_HOST} ^short.co$ [NC]
RewriteCond %{HTTP_HOST} ^www.short.co$ [NC]
RewriteRule ^(.*)$ http://www.some-long-domain.co.uk/$1 [R=301,QSA,L]

advanced .htaccess mod_rewrite from domain to subdirectory

I've found a few threads with similar issues, but haven't had any luck modifying the answers to try and meet the requirements of my site. I have a primary domain, and re-write rules in place to redirect www domain to non-www domain, mapped like this:
primarydoamin.uk => primarydomain.uk
www.primarydomain.uk => primarydomain.uk
RewriteCond %{HTTP_HOST} ^primarydomain.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.primarydomain.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.primarydomain.uk$
RewriteRule ^(.*)$ primarydomain.uk/$1 [R=301,L]
I've a number of add-on / mapped domains against my hosting and I'd like to rewrite based upon a pattern:
drop 'www'
rewrite to a sub-directory of the primary domain (with periods '.' replaced to dashes '-')
only execute when the above rule, redirecting www domain to non-www domain traffic is not met, for example:
www.fishandchips.co.uk => primarydomain.uk/closed/fishandchips-co-uk
www.mushypeas.com => primarydomain.uk/closed/mushypeas-com
pukkapie.uk => primarydomain.uk/closed/pukkapie-uk
Currently I'm copying and pasting the following logic:
RewriteCond %{HTTP_HOST} ^(www.)?fishandchips.co.uk$
RewriteRule ^(.*)$ http://primarydomain.uk/closed/fishandchips-co-uk [R=301,L]
Your first set of rules can be resumed as:
RewriteCond %{HTTP_HOST} ^(www\.)?primarydomain.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.primarydomain.uk$ [NC]
RewriteRule ^ http://primarydomain.uk%{REQUEST_URI} [R=301,L]
For your second set of rules I would try something like:
# This one will work for domains with single extension like .com
RewriteCond %{HTTP_HOST} ^www\.([^\.]+)\.([^\.]+)$ [NC,OR]
RewriteCond %{HTTP_HOST} ^([^\.]+)\.([^\.]+)$ [NC]
RewriteCond %{HTTP_HOST} !primarydomain.uk$ [NC]
RewriteRule ^ http://primarydomain.uk/closed/%1-%2%{REQUEST_URI} [R=302,L]
# This one will work for domains with 2 extensions like .co.uk
RewriteCond %{HTTP_HOST} ^www\.([^\.]+)\.([^\.]+)\.([^\.]+)$ [NC,OR]
RewriteCond %{HTTP_HOST} ^([^\.]+)\.([^\.]+)\.([^\.]+)$ [NC]
RewriteCond %{HTTP_HOST} !primarydomain.uk$ [NC]
RewriteRule ^ http://primarydomain.uk/closed/%1-%2-%3%{REQUEST_URI} [R=302,L]
The %{REQUEST_URI} at the end will ensure it passes down any URL it was coming from that domain.
NOTE: Keep in mind this is an untested solution. It should work as expected, however it does not take into account multilevel sub domains into it, which you will have to adjust as you go.
Also I have set it to 302 instead of 301, use 302 until you confirm its fully working then move it to 301 and also make sure you're using a different browser or cleared your cache to make sure you're not viewing a cached redirect.

Htacces wildcard subdomain problem

Hello I have a problem with wildcard subdomains with htaccess and mod_rewrite.
I want www.domain.com/index.php?foo=x&bar=y translated to x-y.domain.com
My htaccess looks like this
RewriteCond %{HTTP_HOST} !^www.domain.com$
RewriteCond %{HTTP_HOST} ^(.*?)-(.*?).domain.com$
RewriteRule (.*) /index.php?foo=%1&bar=%2
This work but with one minor issue, if I go to x-y.domain.com. Its show www.domain.com, but if I go to x-y.domain.com/z ( 'z' can be every value, even 'asdfasdfasdfasdfasdf' ) it shows the right page, my application doesnt use z for anything its just there for no reason, but its necessary to show the right page.
Solution
RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteCond %{HTTP_HOST} ^(.*?)-(.*?).domain.com$ [NC]
RewriteRule (.*) index.php?foo=%1&bar=%2 [NC]
I dont know why it works but it works

Using htaccess to redirect requests from non-www to www (but with a twist)

I'm currently using htaccess to force all requests to use www. So:
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
to redirect example.com/page to www.example.com/page.
However, this htaccess file is being used by several international versions of example.com. What code do I want that can also do the following:
example.de/page -> www.example.de/page
and
example.co.uk/page -> www.example.co.uk/page
etc.
Potentially, there could be dozens of versions of example.com, so I'm looking to avoid having to remember to edit htaccess every time we add a new country.
You can use server variables in your substition, so this should be doable with
RewriteRule ^(.*)$ http://www.${HTTP_HOST}/$1 [L,R=301]
Right: think I've actually got it cracked. (Or at least it looks like it works):
RewriteCond %{HTTP_HOST} ^example\.(.*)$ [NC]
RewriteRule ^(.*)$ http://www.example\.%1/$1 [R=301,L]
seems to do the trick.
Test if the current host name does not begin with www. and add it if missing:
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Resources