Y
Y
Ysery2020-04-30 11:38:09
htaccess
Ysery, 2020-04-30 11:38:09

What will be the command in .htaccess?

Dear experts.

In the logs I often see that they are trying to access the site's articles using "crooked" links, with various appendages, I want to fix this.

There is this line

RewriteRule ^(.+?)\.html(?!/amp/).+$ https://%{HTTP_HOST}/$1.html [L,R=301]


It strips extra characters after .html (excluding .html/amp/). But it does not cut off GET requests, i.e. requests like this .html?<anything can be here> .

How to modify the command so that all GET requests are also cut off, except for this one ...html?srp=xxxxx&srk=yyyyy

xxxxx and yyyyy, of course, can take any values.

PS
I think it's worth writing about the link structure on the site, it's the following:

https://site.ru/material.html - ссылки на материалы такие
    
https://site.ru/material.html/amp/ - ссылки на AMP (используются для мобильной выдачи Google) материалы такие 
    
https://site.ru/kategoriya/ - ссылки на категории (разделы) такие
    
https://site.ru/tag/imya_tega/ - ссылки на теги, выводятся статьи с указанными в них тегами
    
https://site.ru/manage-subs-comments.html?srp=xxxxx&srk=yyyyy - ссылки на управление подпиской для посетителей, оставивших комментарии

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
dodo512, 2020-04-30
@Ysery

RewriteCond %{QUERY_STRING} !^$
RewriteCond %{QUERY_STRING} !(^|&)srp=
RewriteCond %{QUERY_STRING} !(^|&)srk=
RewriteRule ^(.+?)\.html$ https://%{HTTP_HOST}/$1.html? [L,R=301]

RewriteRule ^(.+?)\.html(?!/amp/).+$ https://%{HTTP_HOST}/$1.html? [L,R=301]

Or merge into one rule
RewriteCond $2 ^(?!/amp/).+ [OR]
RewriteCond &%{QUERY_STRING} ^(?!.*&srp=)(?!.*&srk=)&.+
RewriteRule ^(.+?)\.html(.*)$ https://%{HTTP_HOST}/$1.html? [L,R=301]

V
Viktor Taran, 2020-04-30
@shambler81

RewriteRule ^(.+?)\.html(?!/amp/).+$ https://%{HTTP_HOST}/$1.html [L,R=301]

replace with
RewriteRule ^(.+?)\.html(?!/amp/).+$ https://%{HTTP_HOST}/$1.html? [L,R=301]

GET will be removed
In general, GET is not included in RewriteRule because it is not part of the url!
and this is how they work with GET
# 301 --- http://www.test.com/faq.html?faq=13&layout=bob => http://www.test2.com/faqs.html
RewriteCond %{HTTP_HOST} ^www\.test\.com$
RewriteCond %{QUERY_STRING} (^|&)faq\=13($|&)
RewriteCond %{QUERY_STRING} (^|&)layout\=bob($|&)
RewriteRule ^faq\.html$ http://www.test2.com/faqs.html? [L,R=301]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question