.htaccess make https://www.mysite.com go to https://mysite.com - .htaccess

I'd like my htaccess file to make any url gone to with a www to be removed...
For example, if I was to go to:
https://www.mysite.com
I'd need to be redirected to:
https://mysite.com
The site is SSL encrypted, so all URL's should have https://

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^https://www\.example\.com$ [NC]
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]
</IfModule>

You can use this rule in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(example\.com)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,R=301,NE]

Related

What is the code to be added to Redirect from http to https in .htaccess file?

What is the code to be added to Redirect from http to https in .htaccess file?
here is the existing code in .htaccess file
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/NextNext/
RewriteRule ^(.*)$ /public/$1 [L]
</IfModule>
awaiting your help..
Try this rule after RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

.htaccess multiple domain redirect to once https without www

i have few domains:
www.xxx.com
xxx.com
www.xxx.co
xxx.co
www.xx.io
xxx.io
how to redirect all domains to https://xxx.io?
I have a redirect to https without www:
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Use this rule in your .htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www\.)?example.io [NC]
RewriteRule ^(.*)$ https://example.io%{REQUEST_URI} [L,R=301]
The condition checks to see if the URL is either www.example.io or exmaple.io and if it isn't, then it will rewrite to it.
I've used R=301 which is a permanent redirection. For testing purposes, I advise you change this to R or R=302, which is temporary.
Make sure you clear your cache before testing this.
you can add the following lines to your .htaccess file located at the root of your xxx.io domain:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^xxx.com$ [OR]
RewriteCond %{HTTP_HOST} ^xxx.co$
RewriteRule (.*)$ http://xxx.io/$1 [R=301,L]
</IfModule>

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>

htaccess redirect subdomain

I have the following .htaccess code:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.mysite.co$ [NC]
RewriteRule ^(.*)$ http://www.mysite.co/$1 [R=301,QSA,L]
</IfModule>
The problem is that all subdomains get redirected to www.mysite.co/subdomain
How can I aviod this?
Your rewrite rule logic in plain speak is doing the following:
For ANY HOST other thant www.mysite.co (including foo.mysite.com, blog.mysite.com, etc..)
Permanent Redirect (301) to http://www.mysite.co/
This is the last rule to check in the .htaccess file, so bail out
To not redirect your own subdomains, the easiest and clearest way is to handle it explicitly with more rewrite conditions (see example below). For more kung fu htaccess fun try this: .htaccess Wildcard Subdomains
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.mysite.co$ [NC]
RewriteCond %{HTTP_HOST} !^blog.mysite.co$ [NC]
RewriteCond %{HTTP_HOST} !^foo.mysite.co$ [NC]
RewriteRule ^(.*)$ http://www.mysite.co/$1 [R=301,QSA,L]
</IfModule>
You need to run your index.php file
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9-]+)\.example\.com$ [NC]
RewriteRule !^index\.php($|/) index.php/accounts/%2%{REQUEST_URI} [PT,L]

Codeigniter url rewrite like subdomain url with .htaccess

Currently my Url is: http://www.domain.co.uk/index.php/city/details/city-name
I would like to change it to:
http://www.city-name.domain.co.uk/index.php/city/details/city-name
or:
http://www.city-name.domain.co.uk/city/details/city-name
Put the .htaccess file into the http ://www.domain.co.uk/ document root
To http ://www.city-name.domain.co.uk/index.php/city/details/city-name
RewriteRule ^(.*)/([^/]+)$ http://www.$2.domain.co.uk/$1/$2 [R=301,L]
To http ://www.city-name.domain.co.uk/index.php/city/details/city-name
RewriteRule ^index.php/(.*)/([^/]+)$ http://www.$2.domain.co.uk/$1/$2 [R=301,L]
If the server is the same, set above RewriteRule this line to prevent redirection loop
RewriteCond %{HTTP_HOST} !^www\.(.*).domain\.co\.uk [NC]
File content example
<ifModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.(.*).domain\.co\.uk [NC]
RewriteRule ^index.php/(.*)/([^/]+)$ http://www.$2.domain.co.uk/$1/$2 [R=301,L]
</IfModule>
To exclude domain.co.uk (whitout www)
<ifModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.(.*).domain\.co\.uk [NC]
RewriteCond %{HTTP_HOST} !^domain\.co\.uk [NC]
RewriteRule ^index.php/(.*)/([^/]+)$ http://www.$2.domain.co.uk/$1/$2 [R=301,L]
</IfModule>

Resources