Solved

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

Posted on 2015-01-15
6
118 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
[X]
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
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

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). #…
It is possible to boost certain documents at query time in Solr. Query time boosting can be a powerful resource for finding the most relevant and "best" content. Of course the more information you index, the more fields you will be able to use for y…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…

696 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