Solved

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

Posted on 2015-01-15
6
108 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 12

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 12

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
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 

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 16

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

As Wikipedia explains 'robots.txt' as -- the robot exclusion standard, also known as the Robots Exclusion Protocol or robots.txt protocol, is a convention to prevent cooperating web spiders and other web robots from accessing all or part of a websit…
If your site has a few sections that need to be secure when data is transmitted between the server and local computer, such as a /order/ section for ordering or /customer/ which contains customer data, etc it would of course be recommended to secure…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

708 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now