Apache HTTP to HTTPS redirect please.

Hello experts.

I need some help with an Apache HTTP to HTTPS redirect please.

In my document root I have many php website files.  By default they are served by HTTP.  

However I need 3 specific files and only those 3 files converted to https when the user tries to access them.


Apache already has a self signed certificate installed.

Lets assume the site is called www.example.com

Can you please help with how I do this ?

Thank you.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Brian TaoSenior Business Solutions ConsultantCommented:
This should work.  Don't forget to replace the domain name.
Redirect permanent /index.php https://www.your-domain.com/index.php
Redirect permanent/identification.php https://www.your-domain.com/identification.php
Redirect permanent /register.php https://www.your-domain.com/register.php

Open in new window

Dave BaldwinFixer of ProblemsCommented:
For specific files, I do that in PHP like this.  This is to make sure the domain name matches the SSL/TLS certificate and works with all of the following files in the shopping cart.
$protocol = (@$_SERVER["HTTPS"] == "on") ? "https://" : "http://";

if (substr($_SERVER['HTTP_HOST'], 0, 4) !== 'www.') {
    header('Location: '.$protocol.'www.'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Try this Apache configuration, notice mod_alias has to be enabled.
RedirectMatch 301 ^/((index.php)|(identification.php)|(register.php))$ https://www.exapmle.org/$1

Open in new window

You could also use ModRewrite:
RewriteEngine   on
RewriteCond     %{SERVER_PORT} ^80$
RewriteRule     ^(.*)$ https://%{SERVER_NAME}$1 [L,R]

Open in new window

to enable mod rewrite run:
sudo a2enmod rewrite
sudo service apache2 restart

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Apache Web Server

From novice to tech pro — start learning today.