R
R
Ruslan2021-11-18 12:39:07
htaccess
Ruslan, 2021-11-18 12:39:07

How to write .htaccess correctly?

Hello!
There is a website, it also has an SSL certificate. If I enter https://domain.ru in the browser line like this , then it works fine, and the browser does not issue a warning. And if I just enter domain.ru, then it does not automatically redirect to https and a warning pops up. In .htaccess now I can’t register a redirect, because it is installed on the Apache server and a cyclic redirect occurs.
Now it's written like this

RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vasily, 2021-11-22
@xnscripter

You only redirect from www. on https://withoutwww
I have something like this:

# Перенаправление с www
  RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
  RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

  # Добавляем слэш
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_URI} !\..+$
  RewriteCond %{REQUEST_URI} !(.*)/$
  RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1/ [R=301,L]
  
  # Перенаправление с http на https 
  RewriteCond %{HTTPS} off
  RewriteCond %{HTTP:X-Forwarded-Proto} !https
  RewriteCond %{REQUEST_URI} !^/1c_exchange.php
  RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
  
  # Склейка файлов index
  RewriteRule ^(.*)index(\.(php|html?))?$ https://%{HTTP_HOST}/$1 [R=301,L]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question