NamePros
Welcome, Guest! Ready to make a name for yourself in the domain business? We welcome both the hobbyist and professional domainer to join the discussion as part of the NamePros community.

Click here to create your profile to start earning reputation for posting, and trader ratings for buying & selling in our free e-marketplace. Build your trader rating with each successful sale. Our system has tracked over 100,000 sales and counting!
FAQ & TOS Register Search Today's Posts Mark Forums Read

Go Back   NamePros.com > Website Development Discussion Forums > Programming
Reload this Page htaccess redirect to HTTPS on select pages

Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics.

Advanced Search


Closed Thread
 
LinkBack Thread Tools
Old 07-31-2008, 03:15 PM THREAD STARTER               #1 (permalink)
Senior Member
 
nasaboy007's Avatar
Join Date: Jul 2005
Location: NJ
Posts: 1,219
nasaboy007 has much to be proud ofnasaboy007 has much to be proud ofnasaboy007 has much to be proud ofnasaboy007 has much to be proud ofnasaboy007 has much to be proud ofnasaboy007 has much to be proud ofnasaboy007 has much to be proud ofnasaboy007 has much to be proud ofnasaboy007 has much to be proud of
 



htaccess redirect to HTTPS on select pages


I'm a complete neophyte at using the .htaccess file, but I know that you can do redirections and stuff with it. Now, I want to switch between http and https depending on what file is requested. If they go to the index of the site (http://www.mysitememberships.com OR http://www.mysitememberships.com/index.php), I want it to stay as http. However, if they go to any of, say 4, specific pages, I want it to switch over to https (pages would be /myadd.php, /myedit.php, /myview.php, /userlogin.php, /controlpanel.php). However, after they visit that page and it switches over to https, I would like it to switch back to http if they request a page that is not part of that list (such as going back to the index).
????: NamePros.com http://www.namepros.com/programming/498820-htaccess-redirect-to-https-select-pages.html

How would I go about doing this?
nasaboy007 is offline  
Old 08-01-2008, 09:41 PM   #2 (permalink)
NamePros Regular
 
Palyriot's Avatar
Join Date: Jul 2004
Location: Seattle, Wa
Posts: 596
Palyriot is a jewel in the roughPalyriot is a jewel in the roughPalyriot is a jewel in the rough
 



I'd say just do it in php and not your htaccess file. Just do a conditional that includes the 's' in https:// when those certain pages are linked to.
Palyriot is offline  
Old 08-03-2008, 12:38 PM THREAD STARTER               #3 (permalink)
Senior Member
 
nasaboy007's Avatar
Join Date: Jul 2005
Location: NJ
Posts: 1,219
nasaboy007 has much to be proud ofnasaboy007 has much to be proud ofnasaboy007 has much to be proud ofnasaboy007 has much to be proud ofnasaboy007 has much to be proud ofnasaboy007 has much to be proud ofnasaboy007 has much to be proud ofnasaboy007 has much to be proud ofnasaboy007 has much to be proud of
 



yeah i did it with php, thanks.

my solution, put on every page:

PHP Code:
$httpsfile[] = "1"//placeholder, idk why it doesn't read first array val
$httpsfile[] = "page1.php";
$httpsfile[] = "page2.php";
$httpsfile[] = "page3.php";
$httpsfile[] = "page4.php";
$httpsfile[] = "page5.php";
$httpsfile[] = "page6.php";
$httpsfile[] = "page7.php";

if(
$_SERVER['HTTPS'] != "on" && array_search(basename($_SERVER['PHP_SELF']), $httpsfile) != "") {
    
reload("https://" $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']);
}
else if(
array_search(basename($_SERVER['PHP_SELF']), $httpsfile) == "" && $_SERVER['HTTPS'] == "on") {
????: NamePros.com http://www.namepros.com/showthread.php?t=498820
    
reload("http://" $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']);
????: NamePros.com http://www.namepros.com/showthread.php?t=498820


since it redirects non-https-needed pages back to http, it saves server processing.
nasaboy007 is offline  
Old 08-04-2008, 07:26 AM   #4 (permalink)
NamePros Regular
 
qbert220's Avatar
Join Date: Jul 2007
Location: UK
Posts: 395
qbert220 is a splendid one to beholdqbert220 is a splendid one to beholdqbert220 is a splendid one to beholdqbert220 is a splendid one to beholdqbert220 is a splendid one to beholdqbert220 is a splendid one to beholdqbert220 is a splendid one to behold
 


Protect Our Planet
Originally Posted by nasaboy007
PHP Code:
$httpsfile[] = "1"//placeholder, idk why it doesn't read first array val 
It does read the first entry in the array. array_search returns the key of the matching array entry. The key of the first entry will be zero. != and == are boolean operators. Zero is false when cast to a boolean, all other numbers are true.

array_search will return FALSE if it does not find a match, so you should use === and !== to test without casting like this:

PHP Code:
if($_SERVER['HTTPS'] != "on" && array_search(basename($_SERVER['PHP_SELF']), $httpsfile) !== FALSE) {
    
reload("https://" $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']);
}
else if(
array_search(basename($_SERVER['PHP_SELF']), $httpsfile) === FALSE && $_SERVER['HTTPS'] == "on") {
    
reload("http://" $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']);

I'd favour using in_array since you don't care what the key is:

PHP Code:
if($_SERVER['HTTPS'] != "on" && in_array(basename($_SERVER['PHP_SELF']), $httpsfile)) {
????: NamePros.com http://www.namepros.com/showthread.php?t=498820
    
reload("https://" $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']);
}
else if(
in_array(basename($_SERVER['PHP_SELF']), $httpsfile) && $_SERVER['HTTPS'] == "on") {
????: NamePros.com http://www.namepros.com/showthread.php?t=498820
    
reload("http://" $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']);

For better performance you might want to do the "in_array" once, then check the $_SERVER['HTTPS'] setting.
qbert220 is offline  
Old 08-16-2008, 10:49 AM   #5 (permalink)
NamePros Regular
 
Rudy's Avatar
Join Date: Jul 2005
Location: United States
Posts: 586
Rudy is just really niceRudy is just really niceRudy is just really niceRudy is just really nice
 


Save a Life
Since the thread I started here (http://www.namepros.com/website-deve...-benefits.html) is evolving into a programming thread, and it is very closely related to this thread, I think I'll post here now with a question.

I have the exact code above that qbert has posted:

Code:
if($_SERVER['HTTPS'] != "on" && in_array(basename($_SERVER['PHP_SELF']), $httpsfile)) {
    reload("https://" . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']);
}
else if(in_array(basename($_SERVER['PHP_SELF']), $httpsfile) && $_SERVER['HTTPS'] == "on") {
    reload("http://" . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']);
}
I have this in a required file that I require on every single page of my website. The problem is, when I go to test this, I'm told that the reload() function doesn't exist (and thus, a fatal PHP error is thrown). Nasaboy, do you have the code for the function too? I guess I might have a go and see if I can write the function to work, or find a native function that does the same thing... I see where you are going with this, but I'm just not exactly sure how to get there.
????: NamePros.com http://www.namepros.com/showthread.php?t=498820

Edit:
I guess I could try a PHP redirect, could I not? This would work just as well, right?
Last edited by Rudy; 08-16-2008 at 10:55 AM.
Rudy is offline  
Old 08-16-2008, 10:56 AM THREAD STARTER               #6 (permalink)
Senior Member
 
nasaboy007's Avatar
Join Date: Jul 2005
Location: NJ
Posts: 1,219
nasaboy007 has much to be proud ofnasaboy007 has much to be proud ofnasaboy007 has much to be proud ofnasaboy007 has much to be proud ofnasaboy007 has much to be proud ofnasaboy007 has much to be proud ofnasaboy007 has much to be proud ofnasaboy007 has much to be proud ofnasaboy007 has much to be proud of
 



oh lol sorry, my bad. i just made a reload function, basically header location. here:

PHP Code:
function reload($destination) {
????: NamePros.com http://www.namepros.com/showthread.php?t=498820
    
$reload 'Location: ' $destination;
    
header($reload);



edit: just saw your edit, yes it is just a php redirect. i just put it into a function because i used it so many times throughout my site i didn't feel like typing everything out (laziness ftw).
nasaboy007 is offline  
Old 08-16-2008, 10:59 AM   #7 (permalink)
NamePros Regular
 
Rudy's Avatar
Join Date: Jul 2005
Location: United States
Posts: 586
Rudy is just really niceRudy is just really niceRudy is just really niceRudy is just really nice
 


Save a Life
Ahh, that makes perfect sense. I see it now. Thanks for this!
Rudy is offline  
Closed Thread


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


Liquid Web Smart Servers  
All times are GMT -7. The time now is 11:36 AM.

Managed Web Hosting by Liquid Web
Domain name forum recommended by Domaining.com Powered by: vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.6.0 Ad Management plugin by RedTyger