/    Sign up×
Community /Pin to ProfileBookmark

.htaccesss 301 Redirect

Hey all,
I have 3 pages for credit card billing and I want users to force using SSL (https) protocol. I know i can use .htaccess file under apache to create a rule for that but i can’t really fing an example or good man that i can understand. (I know its not really PHP but hopefully some one can help me out)

so what i need is soenthin like:
if user goes to <any protocol other then https> && <“www” or “”> mydomain.com/creditcard.php
THEN redirect it to “https://www.mydomain.com/creditcard.php

I have this example but it is a bit other way around:

[code=php]
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain.com$
RewriteRule ^$ http://domain.com/index.php [L,R=301]
[/code]

Thanks for help!

to post a comment
PHP

8 Comments(s)

Copy linkTweet thisAlerts:
@FrankTheTankJul 04.2008 — Actually, you can do this within the php file.

[code=php]if(@$_SERVER['HTTPS'] != 'on')
{
header('Location: https://{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}");
}[/code]


.htaccess Is probably more efficient, but I find this method is more manageable for my total lack of skill with .htaccess. It's also convenient if you need to switch it off for a sec... just comment out the header line.
Copy linkTweet thisAlerts:
@alexusauthorJul 04.2008 — yey i didnt think of that one =)

and also i think unlike 301 redirects, this one will not hurt google runkings =)

thanks
Copy linkTweet thisAlerts:
@FrankTheTankJul 04.2008 — Ideally, the condition is never met and the page never gets loaded by that header. But then, you never know when some jerk^h^h^h^h user is going to change their URL.

You can also use this method to add in the www. to the URL - just check on the $_SERVER['HTTP_HOST'] and if it doesn't start with "www." then you can stick it in there. Or just hard code that value and use $_SERVER['REQUEST_URI'] to fill in the file name. This only needs to be so portable, and it beats buying another SSL certificate.

Frank
Copy linkTweet thisAlerts:
@alexusauthorJul 04.2008 — wait I dint get the part with changing URL

what do you mean?
Copy linkTweet thisAlerts:
@FrankTheTankJul 04.2008 — That's for your certificate - your first post made it sound like your certificate is for www.yourdomain.com, and you'll want to force that along with the https protocol. You can do that with an additional condition in the if statement, and a little extra code.

[code=php]if( (@$_SERVER['HTTPS'] != 'on') || (strpos($_SERVER['HTTP_HOST'], 'www.') === false) )
{
$mydomain = 'www.yourdomain.com';
header("Location: https://$mydomain{$_SERVER['REQUEST_URI']}");
}
[/code]


If the protocol isn't https or the URL isn't the www.yourdomain.com, get the page as https://www.yourdomain.com. It solves both problems at once.

Note - I had a typo in my first example, which is now fixed in this bit of code.

Does that clear things up?

Frank
Copy linkTweet thisAlerts:
@alexusauthorJul 04.2008 — oh i got it.. u ment the difference for the plain domain name and www subdomain =)

I will try the code in few minutes when I finish figuring out how da hell make the gmaill to accept html messages and from header... ?
Copy linkTweet thisAlerts:
@FrankTheTankJul 04.2008 — Post a new thread ?
×

Success!

Help @alexus spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.5,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...