U
U
ua302019-03-01 14:36:06
URL Handling
ua30, 2019-03-01 14:36:06

How to make mod_rewrite a single redirect with multiple conditions?

Good afternoon!
There is .htaccess with the following rules:

# на https
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

# c www на без www
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

# без многократных слешей
RewriteCond %{THE_REQUEST} //
RewriteRule .* /$0 [R=301,L]

# в нижнем регистре
RewriteCond %{REQUEST_URI} [A-Z]
RewriteRule (.*) ${lc:$1} [R=301,L]

If a request matches multiple rules, multiple consecutive redirects can be performed. For example, in the worst case it will be:
1. Redirect to https;
2. Redirect to without www + without repeating slashes;
3. Redirect to url in lower case.
Is there a way to do this with a single redirect?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Viktor Taran, 2019-03-01
@shambler81

1. very simple, you don’t need to do this, sequential codes are normal, but it’s better not to do this, but in fact there’s no getting away from them, and Yandex and Google can easily see them.
2. The complexity of such a redirect will grow and, moreover, in terms of the number of freedoms (mat part)
, that is, if I write it to you, you still won’t be able to do something with it later.
3. # in lowercase - give in the face of the one who suggested making this idiotic redirect, Linux is a case-sensitive system, unlike Windows, and such idiotic redirects can be made in it: A no sense, B impossible.
(I’ll explain here)
You will need to make conditions that this is not a file, not a link, not a directory, and only under such a condition change case, otherwise you risk getting 404 from Image.jgp
BUT, in fact, you will only have those URLs that the CNC of the site makes, and forgive me, you need to set up the CNC on the site adequately, that's all, you don't need to suffer .. no.
If you are afraid to make a wrong redirect, then set the [NC] flag.
The formation of the CNC is on the conscience of the engine, climbing into it with idiotic redirects is the height of curvature.
4. .htaccess is read from top to bottom and if you want to redirect to www and it is immediately https, then put it above the special case of a redirect to htttps, in this case you will get a redirect to both www and https at once ;), by moving it below you will really have two redirect.
two slashes are normal BUT, again, the construction will grow by the number of times the degree of freedom, and therefore this is already a minimum of 20 lines

D
dodo512, 2019-03-01
@dodo512

1. Redirect to https;
2. Redirect to without www + without repeating slashes;

RewriteCond %{THE_REQUEST} //     [OR]
RewriteCond %{HTTPS}       off    [OR]
RewriteCond %{HTTP_HOST}   ^www\. [NC]
RewriteRule ^(.*)$ https://site.ru/$1 [R=301,L]

Or like this:
RewriteCond %{THE_REQUEST} //     [OR]
RewriteCond %{HTTPS}       off    [OR]
RewriteCond %{HTTP_HOST}   ^www\. [NC]
RewriteCond %{HTTP_HOST}   ^(?:www\.|)(.+) [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

Or even without a flag[OR]
RewriteCond %{HTTPS};%{HTTP_HOST};%{THE_REQUEST} !^on;(?!www\.)[^;]+;(?!.*//) [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.|)(.+) [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

It is possible to shove everything into one RewriteCond.
RewriteCond %{HTTP_HOST};%{HTTPS};%{THE_REQUEST} ^(?=(?:www\.|)([^;]+))(?!(?!www\.)[^;]+;on;(?!.*//)) [NC]
RewriteRule ^(.*)$ https://%1/$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