A
A
Abai Kerimov2015-07-08 13:45:27
PHP
Abai Kerimov, 2015-07-08 13:45:27

How to make the server accept addresses with a slash at the end and without a slash?

There is a site. Some pages have a slash at the end of the address, and some do not. The task is to configure the server so that it loads pages with and without a slash at the end of the address. The problem is that when you remove the slash or add a slash, it automatically throws you to a 404 page. Help me please.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
E
Evgeny Bukharev, 2015-07-08
@evgenybuckharev

supplement htaccess with the following rules, for SEO it is better to decide whether there are slashes or not:

# Убираем несколько слешей в урле
RewriteCond %{THE_REQUEST} \s/+(.*?)/{2,}([^\s]*)
RewriteRule ^ %1/%2 [R=302,L,NE]
# Убираем последний слеш в урле
RewriteCond %{HTTP_HOST} (.*)
RewriteCond %{REQUEST_URI} (/+)$ [NC]
RewriteRule ^(.*)(/)$ $1 [L,R=301]

S
Stalker_RED, 2015-07-08
@Stalker_RED

You didn't provide any details about the server and I can only guess, but it looks like it's not a server issue but your routing issue. Can you do something like this$url = rtrim($url,"/");

S
ShamblerR, 2015-07-08
@ShamblerR

RewriteEngine On
RewriteBase /
Options +FollowSymLinks
############################################################################
#### Убираем повторяющиеся слеши (/) в URL                              ####
############################################################################
RewriteCond %{REQUEST_URI} ^(.*)/{2,}(.*)$
            #Проверяем, повторяется ли слеш (//) более двух раз.
RewriteRule . %1/%2 [R=301,L]
            #Исключаем все лишние слеши.

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

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

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question