A
A
Asker18882020-08-01 16:51:24
Apache HTTP Server
Asker1888, 2020-08-01 16:51:24

How to set up routing in .htaccess, keeping 301 redirects?

Good day!
Please help me set up .htaccess:
There is a SPA on react using react-router-dom.
The hosting is Apache.
Previously, it was configured like this for the 301st redirect:

RewriteEngine On
RewriteCond %{ENV:HTTPS} !on    [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^.*$ https://mysite.ru/$0 [L,R=301]

With this entry, 301 redirects work, but when I tried to route (for example, https://mysite.ru/my_page ), I was redirected to 404. I
added the following lines:
RewriteEngine On
RewriteCond %{ENV:HTTPS} !on    [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
# Если по переданному пути в URI файлы или каталоги существуют то сервер вернет их содержимое
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
# Если существующих файлов не найдено то сервер вернет содержимое index.html
RewriteRule ^ index.html [L]
RewriteRule ^.*$ https://mysite.ru/$0 [L,R=301]

With such an entry, routing works, but 301 redirects do not work.
How to set up correctly?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Official account of the Timeweb team, 2020-09-17
@Asker1888

try to set up a 301 redirect in one line through the "Redirect" construct, and also swap the order of the rules in .htaccess (it may be that one rule overlaps another)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question