Go Back   CodingForums.com > :: Server side development > Apache configuration

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-27-2012, 04:08 PM   PM User | #1
Jeru
New to the CF scene

 
Join Date: Jan 2012
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
Jeru is an unknown quantity at this point
stripping www from URL with .htaccess

I would like to redirect all users to https. This works fine with this code.

Code:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
# redirect if it looks like this https://domain.com/svrduzx8xCupDgJHpOdq 
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.* - [L]
RewriteBase /
RewriteRule ^([a-zA-Z0-9]+)$ https://%{HTTP_HOST}/application.php?i=$1
But the problem is that some users still type www, resulting in https://www.domain.com. This gives a ssl cert error.
So I would like to first strip the www before redirecting to https. I've tried different approaches, such as:

Code:
# remove www from host
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTP_HOST} ^www\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [L,R=301]
But I can't get it to work. .htaccess is pretty new to me, anyone with experience who knows how to get this done (properly)?
Jeru is offline   Reply With Quote
Old 01-28-2012, 07:41 AM   PM User | #2
abduraooft
Supreme Master coder!

 
abduraooft's Avatar
 
Join Date: Mar 2007
Location: N/A
Posts: 14,680
Thanks: 158
Thanked 2,182 Times in 2,169 Posts
abduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really nice
Try
Code:
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
__________________
Quote:
The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)
abduraooft is offline   Reply With Quote
Old 01-28-2012, 12:18 PM   PM User | #3
Jeru
New to the CF scene

 
Join Date: Jan 2012
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
Jeru is an unknown quantity at this point
Thanks abduraooft for your reply!
Your code works, but only for www.domain.com

It won't redirect https://www.domain.com
Any idea how to tackle this?
Jeru is offline   Reply With Quote
Reply

Bookmarks

Tags
.htaccess

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:39 AM.


Advertisement
Log in to turn off these ads.