Force SSL using .htaccess and mod_rewrite

.htaccess
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

If you want to force secure connection only in certain directory (for example /secure), use this code instead.

.htaccess
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} ^secure
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Comments

  1. RewriteEngine On
    # если запрашиваемый пользователем URL – это не существующий на сервере файл…
    RewriteCond %{REQUEST_FILENAME} !-f
    # …и не существующая на сервере директория
    RewriteCond %{REQUEST_FILENAME} !-d
    # перенаправляем запрос пользователя на xxx.php
    RewriteRule ^(.*)$ xxx.php [L]

Leave a Reply

You may use simple HTML to add links or lists to your comment. Also use <pre><code class="language-*">...</code></pre> to mark up code snippets. We support language-js, language-markup and language-css for comments.
(Optional)