Redirecting HTTPS to Sub-Domain with .htaccess - php

I want to redirect both
https://website.me/ and https://www.website.me/ to https://es.website.me/
This rule doesn't work
RewriteCond %{HTTPS} !^on$
RewriteRule (.*) https://es.website.me/$1 [R,L]

Use below htaccess
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?website\.me$ [NC]
RewriteRule ^(.*)$ https://es.website.me/$1 [R=301,L]

Try this one for redirection in your case:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^website.me$ [OR]
RewriteCond %{HTTP_HOST} ^www.website.me$
RewriteRule (.*)$ https://es.website.me/$1 [R=301,L]
</IfModule>

Do you want to redirect, or rewrite?
To redirect with a code 301 (permanently moved), make 2 virtual hosts; one for the real site, and one for all the URL's you want to redirect. In the redirect host, add this line:
Redirect 301 / https://es.website.me/

Since you want to redirect only if HTTPS is already used, you must check for it, together with the hostname of course.
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^(www\.)?website\.me$ [NC]
RewriteRule ^ https://es.website.me%{REQUEST_URI} [R,L]
When everything works as it should, you may replace R with R=301. Never test with R=301.

Related

Redirect Apache with RewriteRule http or https non-www url to url format https://www [duplicate]

I've had a look through existing questions, but I haven't really come across anything which works for me.
I'm currently running a site with a Secure SSL certificate. It can be accessed at https://www.example.co.uk a problem is the site can also be accessed at http://www.example.co.uk - I don't want this to be possible. I need it to redirect from http to https.
I found this one snippet of code to use in an .htaccess file.
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.co.uk [NC]
RewriteRule ^(.*)$ https://example.co.uk/$1 [L,R=301]
This works fine when the user enters example.co.uk into their address bar, but I also need to add a conditional statement of some sort so that if the user enters 'www.example.co.uk' or 'http://www.example.co.uk'.
I've tried using the likes of [OR], but this ends up creating server errors.
Any help and suggestions is appreciated.
Cheers.
Try the following:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
Also, you can also redirect based on port number, for example:
RewriteCond %{SERVER_PORT} ^80$
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
This will redirect all requests received on port 80 to HTTPS.
Add the following code in .htaccess file.
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
change example.com with your website domain
URLs redirect tutorial can be found from here - Redirect non-www to www & HTTP to HTTPS using .htaccess file
Try this, I used it and it works fine
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
Try this:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
Source:
https://www.ndchost.com/wiki/apache/redirect-http-to-https
(I tried so many different blocks of code, this 3 liner worked flawlessly)
For me work ONLY this variant:
RewriteCond %{HTTPS} off
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Thanks https://www.reg.ru/support/hosting-i-servery/sajty-i-domeny/kak-dobavit-redirekt/redirekt-s-http-na-https (in Russian)
I try all of above code but any code is not working for my website.then i try this code and This code is running perfect for my website. You can use the following Rule in htaccess :
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
//Redirect http to https
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
//Redirect non-www to www
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
</IfModule>
Change example.com with your domain name and sorry for my poor english.
# Switch rewrite engine off in case this was installed under HostPay.
RewriteEngine Off
SetEnv DEFAULT_PHP_VERSION 7
DirectoryIndex index.cgi index.php
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
# RewriteCond %{HTTP_HOST} ^compasscommunity.co.uk\.com$ [NC]
# RewriteRule ^(.*)$ https://www.compasscommunity.co.uk/$1 [L,R=301]
To redirect http://example.com or http://www.example.com to https://www.example.com in a simple way, you can use the following Rule in htaccess :
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond www.%{HTTP_HOST} ^(?:www\.)?(www\..+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [NE,L,R]
[Tested]
%{REQUEST_SCHEME} variable is available since apache 2.4 , this variable contains the value of requested scheme (http or https), on apache 2.4 you can use the following rule :
RewriteEngine on
RewriteCond %{REQUEST_SCHEME} ^http$
RewriteCond %{HTTP_HOST} ^(www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [NE,L,R]
There are better and more secure ways to make sure that all your traffic goes over https. For example setting up two virtual hosts and redirecting all traffic from your http to your https host. Read more on this in this answer here on security.stackexchange.com.
With setting up a virtual host for redirecting you can send a 301 status (redirect permanently) so the browser understands that all the following requests should be sent to the https server where it was redirected to. Hence no further http requests will be made after the first redirect response.
You should also carefully check the given answers because with the wrong rewrite rules set you might loose the query params from your incoming requests.
If you want to redirect HTTP to HTTPS and want to add www with each URL, use the htaccess below
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
it will first redirect HTTP to HTTPS and then it will redirect to www.
Since this is one of the top results in the search, if you're trying to add http to https redirect on AWS beanstalk, the accepted solution will not work.
You need following code instead:
RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteRule ^.*$ https://%{HTTP:Host}%{REQUEST_URI} [L,R=permanent]
Try this
RewriteCond %{HTTP_HOST} !^www. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
Perfect Code Going to HTML index :
RewriteEngine on
RewriteCond %{HTTP_HOST} ^YourNameWebsite\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.YourNameWebsite\.com$
RewriteRule ^/?$ "https\:\/\/YourNameWebsite\.com\/index\.html" [R=301,L]
Or
Perfect Code Going to PHP index :
RewriteEngine on
RewriteCond %{HTTP_HOST} ^YourNameWebsite\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.YourNameWebsite\.com$
RewriteRule ^/?$ "https\:\/\/YourNameWebsite\.com\/index\.php" [R=301,L]
None of these worked for me, except for this. My site was hosted in https://www.asmallorange.com
RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteCond %{SERVER_PORT} !^443$
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
The below code, when added to the .htaccess file, will automatically redirect any traffic destined for http: to https:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
</IfModule>
If your project is in Laravel add the two lines
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
just below RewriteEngine On. Finally your .htaccess file will look like the following.
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
Visite here to more information
in .htaccess file add following line to restrict http access
SSLRequireSSL
for some reason its inverted in my host
so if i want it to reditect i need to check if https is on and then redirect to https
RewriteCond %{HTTPS} =on
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [L,R=301]
is the code i use

Redirect One Page to http instead of https

I have my .htaccess file like below for force https to all my pages.
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^(www\.)?example\.net
RewriteRule ^(.*)$ https://www.example.net/$1 [R,L]
its working fine as per my requirements. Now I want exclude https for one of my page called demo.html
I do not want https for it.
its need
http://example.net/demo.html
instead of
https://example.net/demo.html
You need to add RewriteCond condition for it.
Check the below code :
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^(www\.)?example\.net
RewriteCond %{REQUEST_URI} !^/demo\.html$ [NC]
RewriteRule ^(.*)$ https://www.example.net/$1 [R,L]
you could make that in same number of lines like this :
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule !^demo\.html$ https://www.%1%{REQUEST_URI} [L,R]
If that is OK , change R to R=301 to get permanent redirection because R alone will be considered as R=302 and that temporary redirection .

I want to redirect specific url to non https using htaccess

my current htaccess is:-
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
</IfModule>
i want to add rule to htaccess for url like https://www.example.com/abc/arg1/arg2 should redirect to http://www.example.com/abc/arg1/arg2
for this https://www.example.com/abc/* it should redirect to non-https format with keeping all arguments.
Use below rule, check after clearing your cache.
RewriteCond %{REQUEST_URI} ^/abc/(.*) [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Edit: Please try with below rule,
RewriteCond %{REQUEST_URI} !^/abc/(.*) [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
In above rule we are appending https to all url which are not starting with abc/(.*) you have to use this rewritecond where your original https rule exists.
If you want to redirect all https requests starting with abc/ to http, you must check for both abc/ and HTTPS
RewriteCond %{HTTPS} on
RewriteRule ^abc/ http://www.example.com%{REQUEST_URI} [R,L]
When everything works as it should, you may replace R with R=301 (permanent redirect). Never test with R=301.
I am not sure how you achieve the job, but the following should be working perfectly for you.
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} ^/abc [NC]
RewriteRule ^(.*)$ http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

best htaccess multi-rewrite rule

I need to find the best rewrite rule for my website...
I have 2 domains example.it and example.com but I have SSL under example.com only.
What I need: each user request should be redirected to https://www.example.com/...
Now I'm using this rule:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?example\.it$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
but if I digit http://example.com its redirect me to https://example.com, not www.
Also if I do a GTMetrix test I got an F value under Avoid landing page redirects:
Avoid landing page redirects for the following chain of redirected URLs.
http://example.it/
http://www.example.com/
https://www.example.com/
how can I solve? Thanks.
You can do all this in a single redirect rule and avoid multiple redirects. Have your .htaccess containing this rule only:
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=301,L,NE]
Make sure to clear your browser cache when testing this.

redirect domain to www on codeigniter

What would be the best way to redirect codeigniter website to www domain? The only way that comes to my mind is to use htaccess for this but can't figure out exactly how. This is the rewrite rule I'm using right now to remove index.php from the url. How should I add the redirection to www domain correctly?
RewriteEngine on
RewriteCond $1 !^(index\.php|images|assets|uploads|robots\.txt|favicon\.png|favicon\.ico)
RewriteRule ^(.*)$ index.php/$1 [L]
See this URL
.htaccess Redirect non-WWW to WWW preserving URI string
Or try it
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www.mysite.com [NC]
RewriteRule (.*) http://www.mysite.com/$1 [R=301,L]
Another example
See this URL
Codeigniter redirects for a new domain
Try it
Try adding the following to the .htaccess file in the root directory of your www.xyz.com site.
RewriteEngine on
RewriteBase /
#redirect www.xyz.com/A/B/controller/function
#www.xyz.com/B/controller/function
RewriteCond %{HTTP_HOST} ^www\.xyz\.com$ [NC]
RewriteRule ^A/(B/[\w]+/[\w]+)$ /$1 [L,NC,R=301]
#redirect www.xyz.com/A/controller/function to
#www.abc.com/controller/function
RewriteCond %{HTTP_HOST} ^www\.xyz\.com$ [NC]
RewriteRule ^A/([\w]+/[\w]+)$ http://www.abc.com/$1 [L,NC,R=301]
Use a CNAME record in your DNS.
This saves an HTTP request so is the most efficient option.
Add another condition under that:
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

Categories