开发者

.htaccess HTTPS Redirect while keeping the URL shortform

开发者 https://www.devze.com 2023-02-06 18:15 出处:网络
Ok I have this Rewrite Rule in my .htaccess: RewriteRule ^settings https://%{HTTP_HOST}/user_settings.php

Ok I have this Rewrite Rule in my .htaccess:

RewriteRule ^settings https://%{HTTP_HOST}/user_settings.php
开发者_运维技巧

This redirects "http://domain.com/settings" to "https://domain.com/user_settings.php".

How do I make it so that it redirects "http://domain.com/settings" to "https://domain.com/settings"? Meaning, how can I redirect a url from HTTP to HTTPS while still keeping the Redirect Short url in .htcaccess?

Thanks!


EDIT: Updating answer after question was clarified.

You can do something like this:

RewriteCond %{HTTPS} !=on
RewriteRule ^settings(/?)$ https://%{SERVER_NAME}%{REQUEST_URI} [R,NC]
RewriteCond %{HTTPS} =on
RewriteRule ^settings(/?)$ /user_settings.php [L,NC]

The first RewriteCond will check if HTTPS is off and the following RewriteRule will only happen if it is.

The first RewriteRule will match either of the following:

  • http://domain.com/settings
  • http://domain.com/settings/

The (/?) allows the trailing forward slash to be optional. The R in [R,NC] forced this to be a redirect, the NC means it's not case-sensitive.

The second RewriteCond will again check HTTPS, but this time to make sure it's on.

If HTTPS is on, and you're at your directory (meaning, the first rewrite was successful), it will then do a rewrite to user_settings.php. The L in [L,NC] means that if this rule is matched, it's the last RewriteRule it should follow.

0

精彩评论

暂无评论...
验证码 换一张
取 消