The implementation is based on the single entry point principle, the contents of the address bar are broken down $routes = explode('/', $_SERVER['REQUEST_URI']) and the results are passed to the handlers.

The address bar has the following form: example.com/11/20 , where 11 is the user id and 20 is the category id.

Entries in .htaccess:

 RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule .* index.php [L] 

There was a problem adding a slash to the end of the line. For example, addresses of the type work correctly.

 example.com/11 example.com/11/ example.com/11/20 

but the problems start when I try to add a slash (and an additional variable) to the end of the line:

 example.com/11/20/ example.com/11/20/page3 

In this case, the index page "loses" css styles, js libraries and display images.

Someone can explain why adding a slash breaks the page, and most importantly, why it happens at a certain level of nesting (for example, you can add a slash like this - example.com/11/ - you can still, and so - example.com/11/20/ - already impossible)? Is this somehow fixable?

UPD.

All routes are routes the same way, i.e. in all cases, index.php loaded, and the variable is assigned the value resulting from parsing the string:

  if(!empty($routes[1])) { //если это не главная страница всего сервиса if(intval($routes[1])) { //если это страница пользователя, а не, например, поиск $user_id = intval($routes[1]) ; if (empty($routes[2])) { //если запрос главной страницы или категории (не контакты, не поиск ...) require "html/index.php"; //some code require "html/index.php"; $page = $routes[3]; 

The problem is partially solved by the fact that in the loadable index.php in the lines of the form href = " .. /css/style.css" removed the transition to the level up, i.e. replaced by href = "/ css / style.css" (no dots ahead). I wrote “partially solved” , because now the code works with something like a slash and without it, but I don’t understand the reasons for this behavior and such selectivity (for the code I ask you not to kick).

  • one
    I think there are no telepaths who can assume what is happening there in your code. From your entire description, it’s obvious that routes get one more element than usual. But how this affects the logic of further processing from the text of the question is not clear. Study the code, see what happens when a single empty element is added to the routes. - teran
  • somewhere in the routes, the processing of the third URL parameter is not registered or incorrectly spelled out - labris
  • one
    В этом случае index-страница "теряет" стили css, библиотеки js и отображаемые изображения. generate absolute addresses for them, not relative ones. - Visman
  • CTRL + U press, and see what's going on there, maybe the wrong address to the files is generated ... - Ruslan
  • @teran, updated information in UPD. - 118_64

1 answer 1

If the problem is only in statics, then you need to specify the full path, since all requests go to ./index.php , and for the browser the path will always be like this example.com/11/20/page3/css/style.css

See also article , section "Conclusion": server behavior when interpreting a slash at the end (for example, internal redirect to the default starting point of a level - to the index.html file), this is a special case of a specific server configuration.

  • this is done, but the question remains: why do queries in the address bar of the form example.com/11/ work correctly, but queries of the form example.com/11/20/ work incorrectly? While the elements of the $ routes array are handled in exactly the same way - in all cases index.php is loaded. - 118_64
  • I answered, FOR BROWSER the path will not be an index, but an URL in the address bar. Server and browser paths are two different things. The root of the browser is /, respectively, rising to a level up from / 11 / get to the root, and from / 11/20 / to / 11 / - ozornick