简体   繁体   中英

redirect http to https for some page in site in APACHE

I want to one of my site's page will use only HTTPS.

I have given manually link to all sites to https .

But I want that if user manually types that page URL with http then it should be redirected to https page.

So if user types:

http://example.com/application.php

then it should be redirected to

https://example.com/application.php

Thanks
Avinash

Here's a couple of lines I used in an .htaccess file for my blog, some time ago :

RewriteCond %{HTTP_HOST}  =www.example.com                        
RewriteCond %{REQUEST_URI} ^/admin*                                     
RewriteCond %{HTTPS} !=on                                               
RewriteRule ^admin/(.*)$  https://www.example.com/admin/$1 [QSA,R=301,L]


Basically, the idea here is to :

  • determine whether the host is www.example.com
  • and the URL is /admin/*
    • Because I only wanted the admin interface to be in https
    • which means this second condition should not be useful, in your case
  • and https is off (ie the request was made as http)

And, if so, redirect to the requested page, using https instead of http.


I suppose you could use this as a starting point, for your specific case :-)

You'll probably just have to :

  • change the first and last line
  • remove the second one


Edit after the comment : well, what about something like this :

RewriteCond %{HTTP_HOST}  =mydomain.com
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$  https://mydomain.com/$1 [QSA,R=301,L]

Basically :

  • using your own domain name
  • removing the parts about admin

Try this rule:

RewriteEngine on
RewriteCond %{HTTPS} !=on
RewriteRule ^application\.php$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

This rule is intended to be used in the .htaccess file in the document root of your server. If you want to use it in the server configuration file, add a leading slash to the pattern of RewriteRule .

use this:

RewriteEngine On
# Turn SSL on for /user/login
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} ^/user/login
RewriteRule ^(.*)$ https://%{HTTP_HOST}$1 [R=301,L]

# Turn SSL off everything but /user/login
RewriteBase /
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^/user/login
RewriteRule ^(.*)$ http://%{HTTP_HOST}$1 [R=301,L]

website

open httpd.conf or .htaccess file (mod_rewrite not required):

# vim httpd.conf

Append following line :

Redirect permanent / https://example.com/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM