LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices

Reply
 
LinkBack Search this Thread
Old 04-18-2010, 08:24 AM   #1
rahmad
Member
 
Registered: Aug 2007
Location: Jordan
Distribution: RHEL, Centos, Debian
Posts: 65

Rep: Reputation: 15
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
 
Old 04-18-2010, 08:45 AM   #2
bathory
Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 10,773

Rep: Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295
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

Last edited by bathory; 04-18-2010 at 08:47 AM.
 
Old 04-18-2010, 09:00 AM   #3
rahmad
Member
 
Registered: Aug 2007
Location: Jordan
Distribution: RHEL, Centos, Debian
Posts: 65

Original Poster
Rep: Reputation: 15
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
 
Old 04-18-2010, 09:08 AM   #4
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,716

Rep: Reputation: 635Reputation: 635Reputation: 635Reputation: 635Reputation: 635Reputation: 635
[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
 
Old 04-18-2010, 09:11 AM   #5
bathory
Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 10,773

Rep: Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295
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

Last edited by bathory; 04-18-2010 at 09:13 AM. Reason: forgot code tags
 
Old 04-18-2010, 09:14 AM   #6
rahmad
Member
 
Registered: Aug 2007
Location: Jordan
Distribution: RHEL, Centos, Debian
Posts: 65

Original Poster
Rep: Reputation: 15
Also did not work. in both cases I notice some url manipulation but it gets stuck on https://myserver.com
 
Old 04-18-2010, 02:13 PM   #7
bathory
Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 10,773

Rep: Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295
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]
 
Old 04-19-2010, 12:53 AM   #8
rahmad
Member
 
Registered: Aug 2007
Location: Jordan
Distribution: RHEL, Centos, Debian
Posts: 65

Original Poster
Rep: Reputation: 15
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??
 
Old 04-19-2010, 02:42 AM   #9
bathory
Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 10,773

Rep: Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295
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?
 
Old 04-19-2010, 06:50 AM   #10
rahmad
Member
 
Registered: Aug 2007
Location: Jordan
Distribution: RHEL, Centos, Debian
Posts: 65

Original Poster
Rep: Reputation: 15
my live environment does not support name based ssl vh....I have old version of openssl installed.
 
Old 04-19-2010, 07:20 AM   #11
bathory
Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 10,773

Rep: Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295
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
 
1 members found this post helpful.
Old 04-19-2010, 09:56 AM   #12
rahmad
Member
 
Registered: Aug 2007
Location: Jordan
Distribution: RHEL, Centos, Debian
Posts: 65

Original Poster
Rep: Reputation: 15
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]
 
Old 04-19-2010, 11:55 AM   #13
bathory
Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 10,773

Rep: Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295
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
 
Old 04-20-2010, 02:12 AM   #14
rahmad
Member
 
Registered: Aug 2007
Location: Jordan
Distribution: RHEL, Centos, Debian
Posts: 65

Original Poster
Rep: Reputation: 15
I confirm it is working if I put the code inside httpd.conf under Directory directive.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Redirect https apache requests to folder not in apache root bax Linux - Enterprise 4 04-12-2010 01:16 PM
redirect http to https awreneau Linux - Server 17 10-14-2008 09:13 AM
Apache https redirect without-www to with-www alitrix Linux - Server 3 07-24-2008 06:11 AM
apache: http redirect for any request going to www.foo.com/* psychobyte Linux - Software 1 04-11-2005 05:57 PM
Apache & Squirrelmail:Redirect to https? mac_phil Linux - Software 1 05-24-2004 03:52 AM


All times are GMT -5. The time now is 11:30 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration