Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

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

Posted on 2015-01-15
6
111 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 14

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 14

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
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 

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 17

Accepted Solution

by:
Lucas Bishop earned 500 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
follow the php mysql tuto and tells me what's wrong! 10 39
Running Guacamole on port 80 5 248
htaccess file 3 85
New OSQA server has a ton of fake users 4 71
If you've heard about htaccess and it sounds like it does what you want, but you're not sure how it works... well, you're in the right place. Read on. Some Basics #1. It's a file and its filename is .htaccess (yes, with a dot in the front). #…
In Solr 4.0 it is possible to atomically (or partially) update individual fields in a document. This article will show the operations possible for atomic updating as well as setting up your Solr instance to be able to perform the actions. One major …
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

839 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