htaccess : combining permanent domain change and stripping of www - .htaccess

I I use the code below to strip the www from my (new) domain. Now I want to redirect the old domain tot the new one too. Can't get it to work. Is it possible to do both?
RewriteCond %{HTTP_HOST} ^www\.skiweather\.eu [NC]
RewriteRule ^(.*)$ http://skiweather.eu/$1 [L,R=301]
RewriteBase /

Just rewrite anything to your new domain:
RewriteCond %{HTTP_HOST} !=skiweather.eu [NC]
RewriteRule ^(.*)$ http://skiweather.eu/$1 [L,R=301]

Related

.htaccess to redirect multiple domains, non-www. and non-https to the one single domain and preserve the path

I have a few old domains that resolve to the new site. I just switched on SSL. The old blog posts from old sites have been merged into the new site with the same paths.
I need .htaccess code to redirect multiple domains, non-www. and non-https to the one single domain with www and https. Essentially, if it doesn't begin https://www.mavencomputers.com.au/ then redirect it to that whilst preserving the rest of the path, ie, not redirecting to home.
I currently have
RewriteCond %{HTTP_HOST} ^artisan\-solutions\.net\.au$ [OR]
RewriteCond %{HTTP_HOST} ^www\.artisan\-solutions\.net\.au$
RewriteRule ^/?$ "https\:\/\/www\.mavencomputers\.com\.au\/" [R=301,L]
RewriteCond %{HTTP_HOST} ^mavencomputers\.com\.au$1
RewriteRule ^(.*)$ "https://www.mavencomputers\.com\.au\/%1" [R=301,L]
You can use this .htaccess code:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.mavencomputers\.com\.au$
RewriteCond %{HTTP_HOST} !^images\.mavencomputers\.com\.au$
RewriteRule ^(.*)$ https://www.mavencomputers.com.au/$1 [L,R=301]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
You can test this file with this tool.

Swapping domain name with htaccess

I'm trying to migrate a website to another domain and hope to have requests on the old domain redirected to the new domain, no matter what they are, and regardless of how the URL is structured.
The code below in my .htaccess file does this for a domain and any directories/pages but does not cover the many subdomains I have.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !domain1.com$ [NC]
RewriteRule ^(.*)$ http://domain2.com/$1 [L,R=301]
How can I add to this in order to convert the following (for example):
http://something.domain1.com/directory/page.php?variable=something
http://something.domain2.com/directory/page.php?variable=something
Try:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?domain1\.com$ [NC]
RewriteRule ^(.*)$ http://domain2\.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain1\.com$ [NC]
RewriteRule ^(.*)$ http://%1.domain2\.com/$1 [L,R=301]

Using ISAPI-Rewrite htaccess to 301 redirect multiple urls for multiple hostnames?

I have a single site within IIS, which runs on 4 different hostnames. The CMS handles that, and displays the correct site based on the incoming hostname.
I need to use ISAPI rewrite to handle all the old urls and 301 redirect them to the new equivalants, this is how I currently deal with redirects in my ISAPI-rewrite .htaccess file
RewriteEngine on
RewriteRule ^post/my-old-page-one$ /my-newer-page-one [R=301]
RewriteRule ^post/my-old-page-two$ /my-newer-page-two [R=301]
My issue is, I need it to check the domain too for the incoming url. As the sites have the same old urls, which now need to go to a different page.
I was hoping I could so this.
RewriteEngine on
RewriteRule ^http://www.siteone.com/post/my-old-page-one$ http://www.siteone.com/my-newer-page-one [R=301]
RewriteRule ^http://www.siteone.com/post/my-old-page-two$ http://www.siteone.com/my-newer-page-two [R=301]
RewriteRule ^http://www.sitetwo.com/post/my-old-page-one$ http://www.sitetwo.com/my-newer-page-one [R=301]
RewriteRule ^http://www.sitetwo.com/post/my-old-page-two$ http://www.sitetwo.com/my-newer-page-two [R=301]
But it doesn't work. Any advice greatly appreciated.
The check of the hostname is performed in a separate condition, like this:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.siteone\.com$ [NC]
RewriteRule ^post/my-old-page-one$ http://www.siteone.com/my-newer-page-one [R=301,NC,L]
RewriteCond %{HTTP_HOST} ^www\.siteone\.com$ [NC]
RewriteRule ^post/my-old-page-two$ http://www.siteone.com/my-newer-page-two [R=301,NC,L]
RewriteCond %{HTTP_HOST} ^www\.sitetwo\.com$ [NC]
RewriteRule ^post/my-old-page-one$ http://www.sitetwo.com/my-newer-page-one [R=301,NC,L]
RewriteCond %{HTTP_HOST} ^www\.sitetwo\.com$ [NC]
RewriteRule ^post/my-old-page-two$ http://www.sitetwo.com/my-newer-page-two [R=301,NC,L]

rewrite www. to non www without specifying the domain?

I was using this code to remove www from my domain:
# remove www
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
My problem is that I usually use subdomains and it doesn't work for them. (www.sub.example.com).
How do I rewrite to remove www whatever the domain is and even if it has subdomains?
Try this:
#********** Remove www from address **********
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,NC]
This is a bit more correct than the other solution.
Edit:
I removed [L] as it isn't applicable here.
Though in most cases [NC] non-case sensitive is recommended.
You could probably use capture groups and combine with rewrites... (Completely untested guess)
# remove www
RewriteCond %{HTTP_HOST} ^www.(.*) [NC]
RewriteRule ^(.*)$ http://$1 [R=301,L]

Remove wildcard subdomains but not existing ones with .htaccess

Hello!
I'm trying to set up my .htaccess file for wildcard subdomains, but I really have no clue how to do that.
I have "domain2" pointing to "domain1" as an alias, which is working perfectly, this is the code I'm using:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www)\.(.*)\.(.*)\.(.*) [NC]
RewriteRule ^(.*)$ http://%2.%3.%4/$1 [R=301,QSA,L]
RewriteCond %{HTTP_HOST} ^(.*\.?)domain2.co\.cc$ [NC]
RewriteRule (.*) http://%1domain1.co.cc/$1 [R=301,L]
I found the www redirect here btw: Optimize htaccess Wildcard Subdomain Code
Now, what I want is all non-existent subdomains to get removed and the ones that exist (like "blog.domain1.co.cc" to stay.
I hope someone can help me with this. Thanks!
RewriteEngine On
#no longer needed
#RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
#RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
#don't redirect blog.example.com, forum.example.com and example.com
RewriteCond %{HTTP_HOST} ^((blog|forum)\.)?example\.com$
RewriteRule .* - [L]
#redirect the rest (including www.) to example.com
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
Try adding the following to your htaccess file.
#if these lines already exist, skip them
RewriteEngine On
RewriteBase /
#if its not www or discussions subdomain
RewriteCond %{HTTP_HOST} !^(www|discussions)\.domain1\.co\.cc$ [NC]
#redirect to www domain
RewriteRule .* http://www.domain1.co.cc%{REQUEST_URI} [R=301,L]
For the following question
redirects subdomains and subdirectories to the other domain, just like that: forum.old.com/thread/12038213 --> forum.new.com/thread/12038213
Try
RewriteEngine On
RewriteBase /
#if domain is old.com
RewriteCond %{HTTP_HOST} ^(.+)\.old\.com$ [NC]
#redirect to new.com
RewriteRule .* http://%1.new.com%{REQUEST_URI} [L,R=301]

Resources