LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Server (https://www.linuxquestions.org/questions/linux-server-73/)
-   -   Apache and problem with redirect between HTTP and HTTPS (https://www.linuxquestions.org/questions/linux-server-73/apache-and-problem-with-redirect-between-http-and-https-4175494113/)

tquang 02-07-2014 02:41 AM

Apache and problem with redirect between HTTP and HTTPS
 
Dear everyone,

I installed Apache 2.2.4 and I have problem with redirect. Please help me and below my config (also what I need to run for my site)

My Apache had configurated with SSL and auto redirect from non SSL to SSL (HTTP > HTTPS)
Code:

<VirtualHost *:80>
        ServerName ABC.COM
        ServerAlias www.ABC.COM
        <Location />
                Redirect permanent / https://ABC.COM/
        </Location>
</VirtualHost>

<VirtualHost *:443>
        ServerName ABC.COM
        ServerAlias www.ABC.COM
        DocumentRoot /home/ABC/www/ABC.COM
        <Directory /home/ABC/www/ABC.COM>
            DirectoryIndex index.html index.php
            Order allow,deny
            Allow from all
        </Directory>
        SSLEngine on
        SSLCertificateFile /usr/local/apache/conf/ssl.crt
        SSLCertificateKeyFile /usr/local/apache/conf/ssl.key
        SSLCertificateChainFile /usr/local/apache/conf/ssl.crt
</VirtualHost>

So, above the config is good and no problem until in this site had link like as: ABC.COM/TEST (ABC.COM/TEST/view?code=12)
At this link, I want to redirect it from HTTPS to HTTP for this link only, but I don't know way to do.

Default is HTTPS for all, but ABC.COM/TEST (also ABC.COM/TEST/view?code=12) is HTTP only

Please help me. Thank so much

TenTenths 02-07-2014 03:03 AM

I use this format:

Code:

<VirtualHost xxx.xxx.xxx.xxx:80>
  ServerName www.mydomain.com
  ServerAlias mydomain.com
  RedirectMatch 301 (.*) https://www.mydomain.com$1
</VirtualHost>


tquang 02-07-2014 03:08 AM

Quote:

Originally Posted by TenTenths (Post 5113166)
I use this format:

Code:

<VirtualHost xxx.xxx.xxx.xxx:80>
  ServerName www.mydomain.com
  ServerAlias mydomain.com
  RedirectMatch 301 (.*) https://www.mydomain.com$1
</VirtualHost>


Thank you, Sir. But I need to redirect 1 sub-link (subfolder, ...) from https (ssl) to http (non ssl). In your config is redirect from non ssl to ssl

TenTenths 02-07-2014 03:23 AM

Ah, my mistake, I've not had my first coffee today :)

bathory 02-07-2014 03:42 AM

@OP
Remove the redirect and use mod_rewrite:
Code:

RewriteEngine On

RewriteCond %{REQUEST_URI} !^/TEST
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}


tquang 02-07-2014 03:51 AM

Quote:

Originally Posted by bathory (Post 5113180)
@OP
Remove the redirect and use mod_rewrite:
Code:

RewriteEngine On
RewriteCond %{REQUEST_URI} !^/TEST
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}


Sorry, I don't understand because as I known your rewrite code will made from URL-NoSSL to URL-SSL (if so, not right as my case), please check it!

bathory 02-07-2014 04:09 AM

Quote:

Sorry, I don't understand because as I known your rewrite code will made from URL-NoSSL to URL-SSL (if so, not right as my case), please check it!
What you mean by that?
The above code will rewrite all http requests to https except from /TEST(.*). Isn't that what you want to do?

tquang 02-07-2014 04:12 AM

Quote:

Originally Posted by bathory (Post 5113201)
What you mean by that?
The above code will rewrite all http requests to https except from /TEST(.*). Isn't that what you want to do?

Ah, sorry, I understood, thank you, Sir

bathory 02-07-2014 04:26 AM

Quote:

Originally Posted by tquang (Post 5113203)
Ah, sorry, I understood, thank you, Sir

If you want to rule out the possibility that someone uses https://ABC.COM/TEST..., you can add in the SSL virtualhost configuration, the following:
Code:

RewriteEngine On

RewriteCond %{REQUEST_URI} ^/TEST
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI}

Regards


All times are GMT -5. The time now is 06:15 AM.