Vincent1
@Vincent1

Почему не работают примеры mod_rewrite?

Хочу познать все тонкости работы mod_rewrite, чтобы не копировать слепо htaccess. Для этого открыл httpd.apache.org/docs/current/rewrite/intro.html и там пример:
RewriteRule "^/foo$" "/bar"
If DocumentRoot is set to /usr/local/apache2/htdocs, then this directive would map requests for example.com/foo to the path /usr/local/apache2/htdocs/bar.

Чтобы заработало как надо я создал папку bar в корне сайта и поместил туда файл index.php, в надежде что он откроется когда я перейду на example.com/foo. Но нет.
Not Found

The requested URL /foo/ was not found on this server.

Почему?

RewriteEngine On включен и работает
DirectoryIndex index.php тоже.
Простые редиректы работают "RewriteRule .* example.com"
  • Вопрос задан
  • 75 просмотров
Решения вопроса 1
Vincent1
@Vincent1 Автор вопроса
Поколдовав немного с регулярными выражениями, я понял что слеш перед foo лишний.
Правильно так:
RewriteRule "^foo$" "/bar"
Вывод: на httpd.apache.org/docs/current/rewrite/intro.html примеры с ошибками.
Ответ написан
Комментировать
Пригласить эксперта
Ответы на вопрос 1
@dodo512
RewriteRule "^/foo$" "/bar"

Вполне рабочее правило в контексте VirtualHost, но для Directory и .htaccess оно не походит.
В контексте VirtualHost строка с которой работает RewriteRule всегда начинается с /
В контексте .htaccess строка с которой работает RewriteRule не начинается с /

Эти отличия в зависимости от того, где определена директива RewriteRule описаны документации.
httpd.apache.org/docs/2.4/mod/mod_rewrite.html#rew...
In VirtualHost context, The Pattern will initially be matched against the part of the URL after the hostname and port, and before the query string (e.g. "/app1/index.html"). This is the (%-decoded) URL-path.

In per-directory context (Directory and .htaccess), the Pattern is matched against only a partial path, for example a request of "/app1/index.html" may result in comparison against "app1/index.html" or "index.html" depending on where the RewriteRule is defined.


Ещё этот момент хорошо изложен в статье https://habr.com/company/sprinthost/blog/129560/
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы