Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Need help redirecting to specific HTTPS URL via .htaccess in Apache

Posted on 2015-01-15
6
Medium Priority
?
123 Views
Last Modified: 2015-02-06
Hello.  I need to redirect requests as follows using an .htaccess file in Apache:

http://waffles.com/  ->  https://waffes.com/
http://www.waffles.com/  ->  https://waffles.com/
https://www.waffles.com/  ->  https://waffles.com/
https://waffles.com/ is cool already and should not be redirected, so I imagine I need a RewriteCond to cover this.

This seems simple, there are tons of examples online, and I'm not a total stranger to regexes, but while I am seeing results each time I modify .htaccess (meaning the server is obviously loading my file), I can't make it do what I want.   I either get no redirect or I put the darn browser in a loop.  

The site is hosted at a provider that only allows me to handle this via .htaccess files.   And the site is using Wordpress, and my images break if all requests don't go specifically to https://waffles.com/.  

Finally, the Wordpress install apparently needs these rules in place as well.   I have these below the rules I have tried, but I suspect they should be more intertwined:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase //
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . //index.php [L]
</IfModule>

All help is very much appreciated.
0
Comment
Question by:izgoblin
  • 3
  • 2
6 Comments
 
LVL 15

Expert Comment

by:Edwin Hoffer
ID: 40551406
Use the below code in your .htaccess file:

RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule (.*) https://waffles.com/$1 [R=301,L]

Open in new window


Hope it help you

Thanks
Edwin
0
 

Author Comment

by:izgoblin
ID: 40551465
Hi Edwin,

Thank you much for that.   I am getting unexpected results with that which I can't yet make sense of, but more importantly, does that cover the scenario below?

https://www.waffles.com/  ->  https://waffles.com/

You see, I am not only redirecting all HTTP traffic to HTTPS, but I am also trying to redirect HTTPS traffic to the preferred hostname.
0
 
LVL 15

Expert Comment

by:Edwin Hoffer
ID: 40551578
You said you want to redirect https://www.waffles.com to http://waffles.com? 

I gave the code for this, this will not allow users to use www or visit without https.
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 

Author Comment

by:izgoblin
ID: 40552162
Thanks for the help.  

The code you offered reads to me like this:

# if the request is not HTTPS
RewriteCond %{HTTPS} off

# redirect all requests to https://waffles.com/<path> with a 301 return code
RewriteRule (.*) https://waffles.com/$1 [R=301,L]

Please tell me if I am reading it wrong.   If I am correct, doesn't this mean that a redirect would never occur if someone requests https://www.waffles.com/ ?    That is, the RewriteRule won't fire if the RewriteCond isn't met, correct?    I am not trying to be difficult or unappreciative; obviously I want to be sure I understand how this works properly.

With the code you reference, I am seeing odd things when I test in Internet Explorer 10.

1)  If I request http://www.waffles.com/ , no redirect occurs.  
2)  If I request http://waffles.com/ , my browser gets first redirected to https://waffles.com/ in a loop until the browser drops out of the loop with an error.
3)  If I request https://waffles.com, the same loop occurs as in #2
4)  If I request https://www.waffles.com/, no redirect occurs

When I say that no redirect occurs, I am judging by both what I see in the Fiddler output as well as that I do not see the URL change in my browser (as I thought I should expect).   Unfortunately this also means that some of my images/links break.

Honestly I am completely perplexed by this behavior given what I believe the code you provided should do and what it should not do.   Any thoughts?
0
 
LVL 18

Accepted Solution

by:
Lucas Bishop earned 2000 total points
ID: 40594594
Try this, I've included the WP code so this can be a full replacement:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase //
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . //index.php [L]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://waffles.com/$1 [L,R=301]
RewriteCond %{SERVER_PORT} 443
RewriteCond %{HTTP_HOST} ^www[.].+$
RewriteRule ^(.*)$ https://waffles.com/$1 [L,R=301]
</IfModule>

Open in new window

0
 

Author Comment

by:izgoblin
ID: 40594620
For whatever reason, what I had based on Edwin's code wasn't working on all browsers.   Another guy ended up working it all out, but in the end, this is the .htsaccess code we went with which now does what we want:

# BEGIN GD-SSL
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{SERVER_NAME} ^waffles\.com$
RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
Header add Strict-Transport-Security "max-age=300"
</IfModule>
# END GD-SSL


# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.+)$
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress
0

Featured Post

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you are a web developer, you would be aware of the <iframe> tag in HTML. The <iframe> stands for inline frame and is used to embed another document within the current HTML document. The embedded document could be even another website.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses
Course of the Month7 days, 21 hours left to enroll

824 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question