V
V
Vincent12018-04-19 15:15:49
Apache HTTP Server
Vincent1, 2018-04-19 15:15:49

Why don't mod_rewrite examples work?

I want to know all the subtleties of mod_rewrite, so as not to blindly copy htaccess. To do this, I opened httpd.apache.org/docs/current/rewrite/intro.html and there is an example: 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. To make it work properly, I created a bar folder at the root of the site and placed the index.php file there, hoping that it would open when I went to example.com/foo. But no. Not Found The requested URL /foo/ was not found on this server. Why? RewriteEngine On is enabled and DirectoryIndex index.php works too. Simple redirects work "RewriteRule .*
RewriteRule "^/foo$" "/bar"







example.com "

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vincent1, 2018-04-19
@Vincent1

After some magic with regular expressions, I realized that the slash before foo is superfluous.
Correctly so: Conclusion: on httpd.apache.org/docs/current/rewrite/intro.html examples with errors.

D
dodo512, 2018-05-20
@dodo512

RewriteRule "^/foo$" "/bar"

Quite a working rule in the context of VirtualHost, but it does not work for Directory and .htaccess.
In the context of VirtualHost, the line with which the RewriteRule works always starts with /
In the context of .htaccess, the line with which the RewriteRule works does not start with /
These differences, depending on where the RewriteRule directive is defined, are described in the documentation.
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 (eg "/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.

This point is also well stated in the article https://habr.com/company/sprinthost/blog/129560/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question