LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-29-2016, 06:57 PM   #1
hydrurga
LQ Guru
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 21 MATE
Posts: 8,048
Blog Entries: 5

Rep: Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925
.htaccess redirect for both canonical https:// and www


I'm hoping that Apache .htaccess script counts as a programming language here. ;-)

I'm currently converting a site to the canonical form https://www.example.com and thus would like to place the relevant code in the site's root .htaccess file to redirect accordingly.

The code I've come up with so far is:

Code:
RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]

RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule (.*) https://www.example.com/$1 [R=301,L]
Now this does the trick. However, it requires a maximum of two 301 redirects before the url attains its canonical form.

Does anybody know of any way of doing this so that there is a maximum of only one redirect?
 
Old 06-29-2016, 11:29 PM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,309
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
Quote:
Originally Posted by hydrurga View Post
Does anybody know of any way of doing this so that there is a maximum of only one redirect?
I'm rusty at this but part of it might be the first [L] you have there. It tells Apache to stop processing the rewrite rules. However, even so you should be able to merge the two RewriteRules into one, as multiple RewriteCond statements can precede the RewriteRule. You'll want a pattern that captures the path but not the host name.

By the way, these configurations should not go in .htaccess unless you don't have access to the vhost's own configuration files. The .htaccess is a work-around for situations where you don't have access to the configuration files themselves. I'm not sure why so many guides out there still mention .htaccess since separate configuration files for the vhosts have been around since the 90's.
 
1 members found this post helpful.
Old 06-30-2016, 04:50 AM   #3
hydrurga
LQ Guru
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 21 MATE
Posts: 8,048

Original Poster
Blog Entries: 5

Rep: Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925
Quote:
Originally Posted by Turbocapitalist View Post
I'm rusty at this but part of it might be the first [L] you have there. It tells Apache to stop processing the rewrite rules. However, even so you should be able to merge the two RewriteRules into one, as multiple RewriteCond statements can precede the RewriteRule. You'll want a pattern that captures the path but not the host name.

By the way, these configurations should not go in .htaccess unless you don't have access to the vhost's own configuration files. The .htaccess is a work-around for situations where you don't have access to the configuration files themselves. I'm not sure why so many guides out there still mention .htaccess since separate configuration files for the vhosts have been around since the 90's.
Thanks Tc. I'll have a play around with the code.

The site's on a shared hosting, hence the use of .htaccess.
 
Old 07-04-2016, 09:55 AM   #4
hydrurga
LQ Guru
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 21 MATE
Posts: 8,048

Original Poster
Blog Entries: 5

Rep: Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925
The solution in the end was quite simple. Thanks to Turbocapitalist for the inspiration.

Code:
RewriteEngine On
RewriteCond %{HTTPS} !=on [OR]
RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
 
  


Reply



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] redirect https non-www Apache rahmad Linux - Server 16 10-20-2016 01:18 AM
Nginx, Horde, Https, Redirect, This webpage has a redirect loop axetone Ubuntu 0 02-08-2015 10:44 PM
[SOLVED] htaccess remove www for subdomain and add www for main domain conflicker Programming 7 07-18-2012 06:23 PM
redirect https://www.domain.com to https://domain.com decenter Linux - Server 4 09-13-2011 10:05 AM
Apache https redirect without-www to with-www alitrix Linux - Server 3 07-24-2008 06:11 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:12 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration