PDA

View Full Version : redirection of https to http


r4yd3n
06-24-2009, 09:08 PM
i want to redirect the users who use https to http because i dont have https installed in my server
i have used this .htaccess

RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI}

now it works if i do http://site.com:443 it redirects to http://site.com

but if i do https://site.com it throws an error

any ideas? i would really appreciate if someone can help me.

tomws
06-24-2009, 10:16 PM
That really shouldn't be a problem since most people don't visit via https by default. That being said, here's how I did it in the opposite direction (http to https) some time ago. Perhaps you can tweak it to fit your needs.

RewriteEngine On
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^/(.*) https://%{SERVER_NAME}/$1 [L,R]

r4yd3n
06-24-2009, 10:27 PM
this is the same thing.. your configuration works almost the same way as mine but still when i do https://site.com it shows error however it works for http://site.com:443 redirects to http://site.com/

tomws
06-24-2009, 10:31 PM
Do you have Apache set to accept https connections?

r4yd3n
06-25-2009, 12:09 AM
i dont have ssl right now still can i enable a fake or temporarlity ssl? or any way that wont show the error?

tomws
06-25-2009, 03:58 AM
Well, I'm not sure if I'm correct, but I'd bet that if Apache isn't set up to accept https connections, then you can't redirect. It's needs to accept on https then redirect. I could be wrong.

This post (http://www.codingforums.com/showthread.php?t=170049) from earlier in the day shows how one of my sites is set up to accept SSL connections.

r4yd3n
06-25-2009, 02:14 PM
thanx alot for your help

ssl is already turned on in the conf file
<IfDefine SSL>
# Defined in /var/cpanel/cpanel.config: apache_ssl_port
Listen 0.0.0.0:443
AddType application/x-x509-ca-cert .crt
AddType application/x-pkcs7-crl .crl
</IfDefine>

thats y it is accepting connections on 443 port. and it redirects perfectly fine when specified with 443 but no luck with https :confused:

tomws
06-25-2009, 04:22 PM
Well, I'm not sure what's happening then.

I've tested the appropriately modified code from my first post above, and it works fine. Here's the munged virtual host conf file for reference.

<VirtualHost *:443>
ServerName vacation.example.com
ServerAdmin webmaster@example.com
DocumentRoot /var/www/vacation.example.com
ErrorLog /var/log/apache2/vacation.example.com_error_log
CustomLog /var/log/apache2/vacation.example.com_access_log combined
SSLEngine on
SSLCertificateFile /etc/apache2/ssl/apache.pem
RewriteEngine On
RewriteCond %{SERVER_PORT} ^443$
RewriteRule ^/(.*) http://%{SERVER_NAME}/$1 [L,R]

<Directory "/var/www/vacation.example.com/">
Order allow,deny
Allow from all
Deny from env=keep_out # see file block-these.inc
</Directory>

</VirtualHost>

<VirtualHost *:80>
ServerName vacation.example.com
ServerAdmin webmaster@example.com
DocumentRoot /var/www/vacation.example.com
ErrorLog /var/log/apache2/vacation.example.com_error_log
CustomLog /var/log/apache2/vacation.example.com_access_log combined

<Directory "/var/www/vacation.example.com/">
Order allow,deny
Allow from all
Deny from env=keep_out # see file block-these.inc
</Directory>
</VirtualHost>


Besides opening 443 in the Apache ports configuration, that's the only domain-specific config. Worked fine in testing here.

I notice CPanel in your text above. Is this with a hosting service? You may want to check their support wiki/docs for http related info. They may have their own master config that's affecting something.

r4yd3n
06-25-2009, 08:09 PM
solved thanx :D

r4yd3n
06-26-2009, 01:37 AM
another small question as i am new to apache i am learning

i want to redirect my users to main if they go on some directory path that does not exist or some file that is not here which throws a 404 not found, simply redirect automatically take them to homepage. is this done through the httpd.conf file too?

tomws
06-26-2009, 03:26 PM
A 404 and a redirect are two different things. I don't know if you can redirect after a 404 via Apache. They're there for a reason: to let the user (and you!) know there's a problem with an address. If they're redirected to the index page, that would suggest there's no problem with the incorrect link.

The better option is to implement a custom error handler (see the Apache docs) for a 404 (and other errors, if wanted). You can point 404 errors to a special page on your site with a better explanation than the default 404 handler. That allows you to maintain the site theme even for error conditions.