.htaccess / redirecting to https://www.example.com

2019-08-05 16:12发布

问题:

I have a domain example.com. I want users to be redirected to https://www.example.com whenever one of the following is typed:

  • http://example.com
  • http://www.example.com
  • https://example.com

I also need to redirect people accessing /asdf (or /asdf/) to /index.php?folder=asdf.

I found some examples of code doing the first part, but there was always a debate about their effectiveness.


Edit: (need some more help! :))

回答1:

Try these mod_rewrite rules:

RewriteEngine on

RewriteCond %{HTTP_HOST} !=www.example.com [OR]
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://www.example.com%{REQUEST_URI} [L,R=301]

RewriteCond $1 !=index.php
RewriteRule ^([^/]+)/?$ index.php?folder=$1 [L]

But I recommend you to just allow one URL notation, the one with or without the trailing slash.