Redirect .com to .org in .htaccess - .htaccess

I have domain.com and domain.org as aliases pointing to the same vhost. How can I use .htaccess to redirect all domain.com requests to domain.org?

You could use mod_rewrite to do this.
RewriteEngine on
RewriteCond %{HTTP_HOST} !^example\.org$
RewriteRule ^ http://example.org%{REQUEST_URI} [L,R=301]
This rule redirects every request that’s not addressed to example.org to the very same.

Redirects all www/non-www of domain.com to domain.org:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.org/$1 [R=301,L]

Related

htaccess file not redirecting a certain url

I added an SSL to my site and have the following htaccess file in place to redirect:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} =off [OR]
RewriteCond %{HTTP_HOST} !^example\.com$
RewriteRule ^(.*)$ "https://example.com/$1" [R=301,L]
</IfModule>
This covers almost everything, so http://www.example.com, http://example.com, and example.com all forward to https://example.com. However, there is one scenario where it does not. If I enter https://www.example.com, it does not redirect to https://example.com and I get a security certificate error. Looking for a solution. Thank you in advance.
cdr6545
Try this
<IfModule mod_rewrite.c>
RewriteEngine On
Options +FollowSymlinks
# To redirect from www to non www (Rewrite www.example.com → example.com)
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule ^(.*) http%{ENV:protossl}://%1/$1 [R=301,NE,L]
# Redirect HTTP to HTTPS automatically (only if not in localhost)
RewriteCond %{HTTP_HOST} !=localhost
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>

How to redirect all website requests to HTTPS with no www

I would like to make it so that all requests to my website are redirected to HTTPS with no www. Right now I apparently have both a www and non www version of my site which is supposed to be bad for seo (duplicate content). I also just added an SSL certificate and need to redirect all traffic to the https version of my site. Below is an example of what I am looking to accomplish:
example.com -> https://example.com
www.example.com -> https://example.com
http://www.example.com -> https://example.com
https://www.example.com -> https://example.com
How would I accomplish this within my htaccess file?
try this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTPS_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
</IfModule>
These rules need to be at the top of the htaccess file in your document root
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !example\.com$ [NC]
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]

.htaccess redirection to specific url

I moved my site from blog.abc.com to www.abc.com
I want to redirect every request made to blog.abc.com to abc.com
For example:
If blog.abc.com/example.html is requested, it should redirect to www.abc.com/example.html, how can I do it via .htaccess only?
You can put this code in your htaccess (in blog.abc.com root folder)
RewriteEngine On
RewriteCond %{HTTP_HOST} ^blog\.(.+)$ [NC]
RewriteRule ^(.*)$ http://www.%1/$1 [R=301,L]
Using mod_rewrite you could do it like this:
RewriteEngine On
RewriteCondition %{HTTP_HOST} ^blog.example.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R,L]
or if blog.example.com has its own vhost you could also use mod_alias:
Redirect / http://example.com/

Redirect non-www subdomain

Using these directives, I can redirect any non-www subdomain to the www subdomain:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www\.|$) [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
What I need to achieve is to redirect the www subdomain only.
For example I want to redirect only blog.myname.com to www.blog.myname.com. The rule in the config above will redirect blog.myname.com to www but it also redirect myname.com to www.myname.com, which is not what I am looking for. I also need to redirect all queried subdomain not just blog.myname.com
How can I do this?
You can use this code to achieve that:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^((?!www)[^\.]*)\.myname\.com$ [NC]
RewriteRule ^ http://www.%1.myname.com%{REQUEST_URI} [L,R=301]
Above code withh redirect all subdomains but will not effect myname.com or www.myname.com.
Try:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^blog\.myname\.com$ [NC]
RewriteRule ^ http://www.blog.myname.com{REQUEST_URI} [L,R=301]

htaccess redirect .co.uk to .com for all pages

Im migrating my website from the .co.uk to the .com but need to setup a 301 redirect so all of the individual pages will still be routed properly.
ie i want http://www.mydomain.co.uk/shopping/product1 to go to http://www.mydomain.com/shopping/product1
I have done this before but for the life of me cannot remember how.
many thanks
paul
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.co\.uk$ [NC]
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [R=301,L]
This redirects both the www and non-www for mydomain.co.uk to www.mydomain.com.
To redirect any (sub-)domain other than mydomain.com, use
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^mydomain.com$ [NC]
RewriteRule ^(.*)$ http://mydomain.com/$1 [L,R=301]
Might be useful if you have other domains pointing to the same website.
It also redirects the www.mydomain.com to mydomain.com.
This is accomplished using a simple rewrite placed in your .htaccess file.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^mydomain.co.uk$ [NC]
RewriteRule ^(.*)$ http://mydomain.com/$1 [L,R=301]

Resources