redirect doamin and sub domains to https://www - redirect

My website is written in CodeIgniter and hosted on Ubuntu 16.04
I have to redirect all domain and sub domains to https://www.
Ex: I want like this
http://com-download.live --> https://www.com-download.live
http://youtube.com-download.live --> https://www.youtube.com-download.live
http://vimeo.com-download.live --> https://www.vimeo.com-download.live
Note: Please suggest me the better way, where should I place redirect rules, In apache virtual host or in .htaccess ?
My Current code placed in virtual host is:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)com-download\.live [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}$1 [R=301,L]
But it's not working properly
http://www.com-download.live redirecting to http://www.www.com-download.live

In my opinion redirecting by htacces ll be easier, sample like:
RewriteEngine On RewriteCond %{SERVER_PORT} 80 RewriteRule ^(.*)$
https://www.yourdomain.com/$1 [R,L]
But take into the possibility of editing this in the future it would be better to set the redirection via the server, less emergency and easier to observe if you have many more domains
Example on console:
sudo nano /etc/apache2/sites-available/000-default.conf
RewriteEngine on
RewriteCond %{SERVER_NAME} =example.com [OR]
RewriteCond %{SERVER_NAME} =www.example.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Related

htaccess redirect all variants to https

Using CWP, I have setup a webserver powered by Apache + NginX + Varnish.
I want the following domains samples to open https variants, i.e. https://example.com
example.com
www.example.com
http://www.example.com
http://example.com
https://www.example.com
https://example.com
Hosted websites are developed using CodeIgniter, so index.php also needs to be bypassed.
My current .htaccess is:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
I have tried:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
RewriteCond %{HTTPS} !=on
But it's not working. The following variants makes websites inaccessible on all variants
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
and tried some other variants also even after referring to articles here at Stack Overflow, none is working for me.
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
says too many redirects
There is no effect of any other variant. http and https works fine in them, but no redirection to https is achieved.

Redirect all traffic to temporary domain with 307 redirection

I'm trying to redirect the traffic to my temporary adress. Im using httaccess file to do that. That's the content of it:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^old.domain$
RewriteRule (.*)$ http://new.domain/$1 [R=307, L]
It works if I enter adress: http://old.domain but it won't work with http://old.domain/somenting. It sends then 500 error. If I change R=307 to R=301 it works but I need 307 redirection.
Those rules work fine for me, but there's a syntax error in your rewrite rule's flags. You can't have a space after the comma:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^old.domain$
RewriteRule (.*)$ http://new.domain/$1 [R=307,L]
# no space here-------------------------------^
You can try this:
RewriteEngine on
RewriteCond %{HTTP_HOST} old\.domain [NC]
RewriteRule ^/(.*)$ http://new.domain/$1 [R=307,L]

URL redirection to subdomain without changing URL in address

I am new to Linux and want to use .htaccess or proxy for the URL redirection leaving URL unchanged with apache2.
Now i want to redirect my main domain http://example.com/test to my subdomain http://test.example.com without changing the URL in address bar.
Kindly help me in giving me some guidelines with .htaccess or proxy or some other..
If you don't want the URL to change you either need a Reverse proxy set-up between the two virtual hosts / domains, or if example.com is just an alias, same IP and served from the same Apache virtual host, you could use an internal rewrite. More info HERE
your can also try this. for more infor go HERE
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule ^/?(index.\php)?$ /category [L]
This only works for requests for http://domain.com/ or http://domain.com/index.php, and you'd get served the content at http://domain.com/category without changing the URL in the address bar. You can probably even lose the RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC] line if you don't need it.
there is also a helpful resource here
RewriteEngine on
RewriteBase /
######## example.com ########
RewriteCond %{HTTP_HOST} ^.*example.com$ [NC]
RewriteCond /www/quux-foo.com/www/root/v1%{REQUEST_URI} -d
RewriteRule ^(.*[^/])$ %{REQUEST_FILENAME}/ [L]
RewriteCond %{HTTP_HOST} ^.*quux-foo.com$ [NC]
RewriteCond %{REQUEST_URI} ^/v1/
RewriteRule ^(.*)$ - [F]
RewriteCond %{HTTP_HOST} ^.*example.com$ [NC]
RewriteCond /www/quux-foo.com/www/root/v1%{REQUEST_URI} -f [OR]
RewriteCond /www/quux-foo.com/www/root/v1%{REQUEST_URI} -d
RewriteRule ^(.*)$ v1/$1 [L]
RewriteCond %{HTTP_HOST} ^.*example.com$ [NC]
RewriteCond %{REQUEST_URI} !^/v1/
RewriteRule ^(.*)$ v1/index.php [L]
######## example.com ########

ISPAI Rewrite 301 Redirects / SEO URL Canonicalization on Windows Server

I am new to working on a windows server and have been made aware that it is setup for ISPAI Rewrite.
I have tried creating a httpd.ini file and a .conf file with possible redirects but nothing is working.
Could someone show me how to create the correct file and then rewrite the following:
http://aspectexhibitions.co.uk/
Should redirect to:
http://www.aspectexhibitions.co.uk/
--
http://www.aspectexhibitions.co.uk/index.php
http://aspectexhibitions.co.uk/index.php
Should redirect to:
http://www.aspectexhibitions.co.uk/
Done...
[ISAPI_Rewrite]
#Redirect Non WWW to WWW
RewriteEngine on
RewriteCond %{HTTPS} (on)?
RewriteCond %{HTTP:Host} ^(?!www\.)(.+)$ [NC]
RewriteCond %{REQUEST_URI} (.+)
RewriteRule .? http(?%1s)://www.%2%3 [R=301,L]
#Redirect index.php to root
RewriteEngine on
RewriteBase /
RewriteRule ^index\.php$ http://www.aspectexhibitions.co.uk/ [NC,R=301,L]

htaccess https redirect only on specific Zend Frameworks controller/actions

I'm new to this community, but I've found it really useful time to time.
I've searched for the answer a lot, but I didn't find anything like I need. I've tried to solve it on my own, but I still get errors, so I hope to find someone that can show me the right way... :-)
I've got a "classic" ZF website, with many controller/action urls that are redirect to index.php with a .htaccess file.
Now, what I need, is to redirect a couple of controller to https ssl connection excluding some actions of both controllers.
The way I was trying to do it is:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^mydomain\.tld [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{HTTPS} on
RewriteCond $1 ^!((member|shop)/(?!(index|login))(.*))
RewriteRule ^(.*) http://%{HTTP_HOST}/$1 [L,R=301]
RewriteCond %{HTTPS} off
RewriteCond $1 ^((member|shop)/(?!(index|login))(.*))
RewriteRule ^(.*) https://%{HTTP_HOST}/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !\.(js|ico|gif|jpg|png|css|swf|pdf|txt)$ index.php
It seems to work when I go to /member controller
But then, when I go to another controller, for example /index or /about it does not redirect to the http connection (port 80), and if I try to change a little the rewrite condition regex it sometimes does a redirect loop and the browser gives me a notice blocking the connection to the site.
Is there anyone that could show me the right synthax to use in my rewrite conditions to allow both the controllers (excluding the given actions) under an https connection and going back to a standard http connection when changing controller?
Thanks in advance.
Alessandro
Try these rules instead (replace appropriate lines):
RewriteCond %{HTTPS} on [NC]
RewriteCond %{REQUEST_URI} !^/(member|shop)/ [NC]
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [L,R=301]
RewriteCond %{HTTPS} off [NC]
RewriteCond %{REQUEST_URI} ^/(member|shop)/ [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [L,R=301]
These rules a bit simple (HTTPS will be applied to ALL URLs in /member/ and /shop/ controllers (e.g. /member/login, /member/dashboard, /member/orders/15423/invoice etc)
Negate ! should be before ^ in RewriteCond directive -- if you want your own rules then replace RewriteCond $1 ^!((member|shop)/(?!(index|login))(.*)) by RewriteCond $1 !^((member|shop)/(?!(index|login))(.*))
A method we use to redirect to https is to leave the default Zend Framework .htaccess settings and create an action helper to redirect to https when required. Our action helper code is:
<?php
class RequireSSL extends Zend_Controller_Action_Helper_Abstract
{
public function direct()
{
if(empty($_SERVER['HTTPS']) && $config['billing']['requireSSL'])
{
$redirector = $this->getActionController()->getHelper('Redirector');
$redirector->goToUrlAndExit('https://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']);
}
}
}
Then when you are in a controller action that you need to be accessed via HTTPS you simply call this function at the beginning:
$this->_helper->requireSSL();
Also, here is another method for using action helpers that is a little more detailed if you need it:
http://juriansluiman.nl/en/article/110/in-control-of-https-for-action-controllers
Hope that helps!
I have discovered where the origin of the problem is, but I'd still need support to understand how to solve it.
I have tested on a local linux machine the htaccess and the result was the same... testing separately the two https condition statements (on and off) they work correctly redirecting basing on the given RewriteCond regex. When putting together only the redirect from http to https works.
Https to http redirect works only if the regex is not matched, else it redirects to http://www.mydomain.tld/index.php
So I finally tried to delete the last htaccess statement and it started to work correctly, but, obviously, it does not find the url, as it does not redirect to the index.php anymore.
It looks like after the correct https redirect the index.php one creates the problem. So I'm asking myself if there is a way to avoid this and make it work correctly.
As I wrote before, this seems to be a common problem of this htaccess, as its behaviour is the same on the test and on the production server (different linux flavours).
I put here the working code:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
#RewriteCond %{HTTP_HOST} ^mydomain\.tld [NC]
#RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{HTTPS} on
RewriteCond $1 !^((member|shop)/(?!(index|login))(.*))
RewriteRule ^(.*) http://%{HTTP_HOST}/$1 [L,R=302]
RewriteCond %{HTTPS} off
RewriteCond $1 ^((member|shop)/(?!(index|login))(.*))
RewriteRule ^(.*) https://%{HTTP_HOST}/$1 [L,R=302]
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteRule !\.(js|ico|gif|jpg|png|css|swf|pdf|txt)$ index.php
Finally I have solved it! I persevered in searching the answer because I think that doing it with .htaccess is cleaner, smarter and easy to maintain.
The problem was essentially due to the regexp used in the "ssl to non-ssl" block that was not correctly matching the value passed (that is best matched now reading the env variable %{THE_REQUEST}, avoiding, in some cases, an erroneus redirects loop.
I paste here the working code for further reference:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteOptions MaxRedirects=1
RewriteCond %{HTTP_HOST} ^yoursite\.tld [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /((controller1|controller2)/(?!(action1|action2))(.*))\ HTTP/
RewriteRule ^(.*)$ https://%{SERVER_NAME}/$1 [L,R=301]
RewriteCond %{HTTPS} on
RewriteCond %{THE_REQUEST} !^[A-Z]+\ /((controller1|controller2)/(?!(action1|action2))(.*))\ HTTP/
RewriteRule ^(.*)$ http://%{SERVER_NAME}/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} \.(js|css|ico|gif|jpg|png|swf|txt|pdf)$ [OR]
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ index.php [NC,L]