LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   .htaccess redirect for both canonical https:// and www (https://www.linuxquestions.org/questions/programming-9/htaccess-redirect-for-both-canonical-https-and-www-4175583389/)

hydrurga 06-29-2016 06:57 PM

.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?

Turbocapitalist 06-29-2016 11:29 PM

Quote:

Originally Posted by hydrurga (Post 5568152)
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.

hydrurga 06-30-2016 04:50 AM

Quote:

Originally Posted by Turbocapitalist (Post 5568212)
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.

hydrurga 07-04-2016 09:55 AM

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]



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