LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Server (https://www.linuxquestions.org/questions/linux-server-73/)
-   -   redirect https non-www Apache (https://www.linuxquestions.org/questions/linux-server-73/redirect-https-non-www-apache-802678/)

rahmad 04-18-2010 08:24 AM

redirect https non-www Apache
 
Hi all,

I need to redirect any request to my server as the following

from https://myserver.com to https://www.myserver.com

I tried the following code but it only worked for http not https


Quote:

Options +FollowSymlinks
RewriteEngine On
RewriteCond %{HTTP_HOST}//s%{https} ^[^w][^w][^w][^.].*//((s)on|s.*) [NC]
RewriteRule ^ http%2://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Please help

bathory 04-18-2010 08:45 AM

Hi,

You can use:
Code:

Options +FollowSymlinks
RewriteEngine On
RewriteCond %{HTTP_HOST}  !^www.myserver.com [NC]
RewriteCond %{HTTP_HOST}  !^$
RewriteRule ^/?(.*)        https://www.myserver.com/$1 [L,R]

If you put it in the SSL configuration part of apache it will do what you want. If you put it into the generic httpd.conf it will redirect http*://myserver.com to https://www.myserver.com.

Regards

rahmad 04-18-2010 09:00 AM

Hi Bathory,

I put your code inside httpd.conf but https://myserver.com didnt go to https://www.myserver.com. however it worked for http://myserver.conf.

Please note I have defined the default server name to: www.myserver.com:443

kbp 04-18-2010 09:08 AM

[untested]
 
Not sure if it will work, but you could give this a try -

Code:

RewriteCond %{HTTPS} ^on$
RewriteCond %{HTTP_HOST} ^myserver.com$
RewriteRule ^(.*)$ https://www.myserver.com/$1 [R,L]

hth

bathory 04-18-2010 09:11 AM

You can put the rewrite stuff inside the <Directory ..></Directory> part of the document root. If your DocumentRoot is /var/www/html, you can use:
Code:

<Directory "/var/www/html">
...
ROptions +FollowSymlinks
RewriteEngine On
RewriteCond %{HTTP_HOST}  !^www.myserver.com [NC]
RewriteCond %{HTTP_HOST}  !^$
RewriteRule ^/?(.*)        https://www.myserver.com/$1 [L,R]

Or, as I've already told you, you can add it also in the SSL part of the apache configuration.

Cheers

rahmad 04-18-2010 09:14 AM

Also did not work. in both cases I notice some url manipulation but it gets stuck on https://myserver.com

bathory 04-18-2010 02:13 PM

Try the following that checks for both SSL and FQDN:
Code:

RewriteEngine on

RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST}  !^www.myserver.com [NC]
RewriteRule (.*) https://www.myserver.com/$1 [L,R]


rahmad 04-19-2010 12:53 AM

This did not work bathory :S

I solved it on my testing environment using 2 name-based ssl vh and i put the following inside myserver.com:443

Quote:

RewriteEngine On
RewriteRule ^(.*)$ https://www.myserver.com$1 [R=301,L]
But Iam looking for a solution rather than this because I dont want to change my apache and openssl version at the time being..

any ideas??

bathory 04-19-2010 02:42 AM

Quote:

But Iam looking for a solution rather than this because I dont want to change my apache and openssl version at the time being..
I don't get it. What has apache and openssl version has to do with this?

rahmad 04-19-2010 06:50 AM

my live environment does not support name based ssl vh....I have old version of openssl installed.

bathory 04-19-2010 07:20 AM

Hi,

Even if it's a production server, you should consider upgrading apache/openssl due to security bugs in older versions.

Regarding the rewrite rules, I guess that both the ssl vhosts (myserver.com and www.myserver.com) use the same docroot. If that's the case, you can put the rewrite stuff from my previous post in a .htaccess in the docroot

Regards

rahmad 04-19-2010 09:56 AM

it is working after putting the code inside .htaccess :).

but why it should be inside .htaccess if the 2 vhs in the same docroot?!

another question. is there will be a side effect problems after using this rules. for example if the link contains special characters like "?" etc...

I am newbie to rewrite..the following is the modified version to only redirect https://myserver.com to https://www.myserver.com and keep http links intact
Code:

RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST}  !^www.myserver.com [NC]
RewriteRule (.*) https://www.myserver.com/$1 [L,R]


bathory 04-19-2010 11:55 AM

Quote:

but why it should be inside .htaccess if the 2 vhs in the same docroot?!
It should work the same regardless if you put the code in .htaccess or in httpd.conf. Maybe you did something wrong.

Quote:

another question. is there will be a side effect problems after using this rules. for example if the link contains special characters like "?" etc...
No, the $1 will be replaced by the actual URI no matter if it contains special characters or not.

Regards

rahmad 04-20-2010 02:12 AM

I confirm it is working if I put the code inside httpd.conf under Directory directive.

ryanrajpoot 10-19-2016 04:12 PM

I have added SSL to my website https://www.pnrstatusbuzz.in/ and redirect http non-www to https www with htaccess but sometime https shows in my domain name and sometimes not on browsers. I just want to know whether it's htaccess redirection code issue or SSL installation issue or anything else. If you ask i can show you my htaccess codes. I have tried all the possible way but none worked. Please guide me what to do and how to solve this issue.


All times are GMT -5. The time now is 02:55 PM.