S
S
skrega2020-11-26 08:04:59
htaccess
skrega, 2020-11-26 08:04:59

How to add a slash at the end of a url?

Good day to all, universal hugs to those who help and thanks. I myself do not rummage for this, we set the task. The site was not made by me, what was written in .htaccess below and what I tried to edit.
What could be the problem, I tried a lot of things, but apparently not all.

https://auto-cartel.ru/ - site if needed

Added this, slashes were added, but all styles were lost.
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\..+$
RewriteCond %{REQUEST_URI} ^(.+)$
RewriteRule ^(.*)$ $1 / [L,R=301]

This was already
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.auto-cartel.ru$ [NC]
RewriteRule ^(.*)$https://auto-cartel.ru/$1 [R=301,L]
RewriteBase /
RewriteCond %{THE_REQUEST} ^[AZ]{3,9}\ /([^.]+)\.html\ HTTP
RewriteRule ^ ([^.]+)\.html$ auto-cartel.ru/$1 [R=301,L]
RewriteCond %{REQUEST_URI} !(\.[^./]+)$
RewriteCond %{REQUEST_fileNAME} !-d
RewriteCond %{REQUEST_fileNAME} !-f
RewriteRule (.*) /$1.html
ErrorDocument 404 /404.html


ExpiresActive On
ExpiresByType text/javascript "access plus 30 day"
ExpiresByType text/css "access 1 month"




Header set Cache-Control " max-age=2592000, public"



5fbf3bd9f0791448259837.png

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Ross Alex, 2020-11-26
@Wacdis

RewriteCond %{REQUEST_URI} !(.+)/$ # Если нет слеша
RewriteCond %{REQUEST_FILENAME} !-f # И это не файл
RewriteRule ^(.+)$ https://%{HTTP_HOST}/$1/ [R=301,L] # Ходи сюда :)

V
Viktor Taran, 2020-11-26
@shambler81

Stupidly, you can’t add a slash, because from a semantic point of view it’s wrong ;
you need to add it if it’s a directory or a virtual directory, and vice versa, remove it if it’s a file
, for example /index.html/ - it looks stupid and semantically incorrect.
So you need to add and remove.
I also corrected the requirements for google page speed.
here you are

RewriteEngine on 
#  Директива включает редиректы.
ErrorDocument 404 /404.html
# 404 ведет на 404.html

############################################################################
#### Выбор основного зеркала (с www или без www)                        ####
############################################################################
 # 1. Удалить www
RewriteCond %{ENV:HTTPS} on
    #Если включен https
RewriteRule .* - [E=SSL:s]
    #То создаем переменную  ssl с текстом s
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
    # Проверяем, содержит ли домен www в начале URL.
RewriteRule ^(.*)$ http%{ENV:SSL}://%1/$1 [R=301,L]
    # Перенаправляем удаляем www

############################################################################
#### Перенаправляем протокол http на https                              ####
############################################################################
RewriteCond %{HTTPS} off
   # Проверяем наличие https в URL.
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
   # Перенаправляем протокол на http.

############################################################################
#### Убираем index.php, если он есть в конце URL                        ####
############################################################################
RewriteCond %{REQUEST_URI} ^(.*)/index\.php$
   # URL cодержит index.php в конце.
RewriteCond %{REQUEST_METHOD} =GET
   # Выявляем GET запрос в URL (не POST).
RewriteRule ^(.*)$ %1/ [R=301,L]
   # Удалить index.php из URL.

############################################################################
#### Убираем повторяющиеся слеши (/) в URL                              ####
############################################################################
RewriteCond %{THE_REQUEST} //
   # Проверяем, повторяется ли слеш (//) более двух раз.
RewriteCond %{QUERY_STRING} !http(s|)://
  # Убедимся что это не урл в  GET
RewriteRule .* /$0 [R=301,L]
   # Исключаем все лишние слеши.

############################################################################
#### Убираем слеши в конце URL для статических файлов (содержит точку)  ####
############################################################################
RewriteCond %{REQUEST_URI} \..+$
   # Если файл содержит точку.
RewriteCond %{REQUEST_FILENAME} !-d
   # И это не директория.
RewriteCond %{REQUEST_FILENAME} -f
   # Является файлом.
RewriteCond %{REQUEST_URI} ^(.+)/$
   # И в конце URL есть слеш.
RewriteRule ^(.+)/$ /$1 [R=301,L]
   # Исключить слеш.

############################################################################
#### Добавляем слеш(/), если его нет, и это не файл.                    ####
############################################################################
RewriteCond %{REQUEST_URI} !(.*)/$
   # Если слеша в конце нет.
RewriteCond %{REQUEST_FILENAME} !-f
   # Не является файлом.
RewriteCond %{REQUEST_URI} !\..+$
   # В URL нет точки (файл).
RewriteCond %{REQUEST_URI} ^(.+)$
 # В URL есть хоть один символы
RewriteRule ^(.*)$ $1/ [L,R=301]
   # Добавляем слеш в конце.


############################################################################
#### если это заглавные буквы от трех до девяти например ВАСЯ.html      ####
############################################################################

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^.]+)\.html\ HTTP
# если это английские заглавные буквы от 3 до 9
RewriteRule ^([^.]+)\.html$ https://auto-cartel.ru/$1 [R=301,L]
# то  удалить html останется просто /ВАСЯ, на другие случаи не распространяется.

############################################################################
#### ЧПУ ДВИЖКА стоит всегда в самом низу правил                        ####
############################################################################
RewriteCond %{REQUEST_URI} !(\.[^./]+)$
RewriteCond %{REQUEST_fileNAME} !-d
RewriteCond %{REQUEST_fileNAME} !-f
RewriteRule (.*) /$1.html

############################################################################
#### Компрессия статического контента для гугл  спид тест               ####
############################################################################
<IfModule mod_deflate.c>
  AddType image/svg+xml .svg
  AddOutputFilterByType DEFLATE image/svg+xml  
  AddOutputFilterByType DEFLATE application/rss+xml
  AddOutputFilterByType DEFLATE application/vnd.ms-fontobject
  AddOutputFilterByType DEFLATE application/x-font
  AddOutputFilterByType DEFLATE application/x-font-opentype
  AddOutputFilterByType DEFLATE application/x-font-otf
  AddOutputFilterByType DEFLATE application/x-font-truetype
  AddOutputFilterByType DEFLATE application/x-font-ttf
  AddOutputFilterByType DEFLATE application/x-javascript
  AddOutputFilterByType DEFLATE application/xhtml+xml
  AddOutputFilterByType DEFLATE application/xml
  AddOutputFilterByType DEFLATE font/opentype
  AddOutputFilterByType DEFLATE font/otf
  AddOutputFilterByType DEFLATE font/ttf
  AddOutputFilterByType DEFLATE image/svg+xml
  AddOutputFilterByType DEFLATE image/x-icon
  AddOutputFilterByType DEFLATE text/css
  AddOutputFilterByType DEFLATE text/html
  AddOutputFilterByType DEFLATE text/javascript
  AddOutputFilterByType DEFLATE text/plain
  AddOutputFilterByType DEFLATE text/xml
  AddOutputFilterByType DEFLATE image/svg+xml
</IfModule>
<IfModule mod_expires.c>
  ExpiresActive on
  ExpiresByType image/jpeg "access plus 1 year"
  ExpiresByType image/svg "access plus 1 year"
  ExpiresByType image/gif "access plus 1 year"
  ExpiresByType image/png "access plus 1 year"
  ExpiresByType text/javascript "access plus 1 year"
  ExpiresByType text/css "access plus 1 year"
  ExpiresByType application/javascript "access plus 1 year"
  ExpiresByType application/vnd.ms-fontobject "access plus 1 year"
  ExpiresByType application/x-font-ttf "access plus 1 year"
  ExpiresByType application/x-font-opentype "access plus 1 year"
  ExpiresByType application/x-font-woff "access plus 1 year"
  ExpiresByType image/svg+xml "access plus 1 year"
</IfModule>
  <IfModule mod_headers.c>
  <filesmatch "\.(ico|flv|jpg|jpeg|webp|png|gif|css|swf|woff|pdf)$">
    Header set Cache-Control "max-age=31536000, public"
  </filesmatch>
  <filesmatch "\.(html|htm)$">
    Header set Cache-Control "max-age=7200, private, must-revalidate"
  </filesmatch>
  <filesmatch "\.(pdf)$">
    Header set Cache-Control "max-age=86400, public"
  </filesmatch>
  <filesmatch "\.(js|otf|ttf|woff|woff2)$">
    Header set Cache-Control "max-age=31536000, private"
  </filesmatch>
  </IfModule>
############################################################################
#### Конец общей части, далее следует собственные директивы .htaccess   ####
############################################################################

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question