Force url path for mod_rewrite Replace RewriteRule

When doing a rewrite with mod_rewrite RewriteRule

, it evaluates and evaluates whether it is a URL or a filesystem path based on whether a rewrite root exists on the filesystem. Here's the relevant section from the documentation :

Url path

Note that mod_rewrite tries to guess if you specified a filesystem path or a URL path by checking if the first segment of the path exists at the root of the filesystem. For example, if you supply a substitution string from /www/file.html

, then it will be treated as a URL path if no directory named exists in the root or your filesystem www

(or, in the case of rewriting in .htaccess

, relative to your document root), in which case it will be considered as the path to the filesystem.

So my question is, how do I rewrite the URI where the root directory exists on the filesystem, but I want it to be treated as a URL?

Is there any way other than specifying the full url? Also according to the docs:

Absolute URL

If an absolute URL is specified, mod_rewrite checks if the hostname is the same as the current host. If so, the schema and hostname are removed and the resulting path is treated as a URL path. Otherwise, an external redirect is performed for the given URL.

So I can get around it this way:

RewriteRule ^/example.html$ %{REQUEST_SCHEME}://%{HTTP_HOST}/var/example.html

      

This is not ideal for reading code. I really want to be able to:

RewriteRule ^/example.html$ /var/example.html

      

Without it, it evaluates to the path to the filesystem, if exists /var

. It seems that this is not a flag. There is also a risk that the match path will be created later and the rules that I would like to avoid will be broken, not many words.

I also know I can solve this by putting rules in .htaccess

instead of Apache config, so the search is done from the document root as described in the quote above, but that's not what I'm looking for.

Is there another way?

Update

Thanks to Dusan Bajic's answer, it turns out that the flag [PT]

might help here, as it hinted in the sentence that I missed the first quote from the docs:

If you want other URL matching directives (such as Alias) to be applied to the resulting URL, use the [PT] flag as described below.

Using the flag [PT]

( docs here ) will cause the substitution to "be treated as a URI instead of [file-path]", which sounds like it should answer this question, but unfortunately there are other implications for use flag [PT]

:

  • This is implied [L]

    , so no further processing of the rewrite rules will occur.
  • This causes the URL mapping to rerun, which is what it is for, and might not be desirable when it just wants to force the lookup to be interpreted as a URL path.

So I leave this question open with no accepted answer at the moment, as the desired solution would not have the above issues. The accepted answer will cause the substitution to be treated as a URL path, as it would if the root of the substitution did not exist on the filesystem, without affecting processing in some other way.

Update 2

I figured out that another option to help with this when the url is inside the document root is to always add %{DOCUMENT_ROOT}

at the beginning of the lookup. So he does it like this:

RewriteRule ^/example.html$ %{DOCUMENT_ROOT}/var/example.html

      

This also prevents later URL processing rules, so it looks like a parameter [PT]

without causing the URL to re-run.

+3


source to share


1 answer


You missed the last sentence of the quoted paragraph:

If you want to apply other URL mapping directives (such as an alias) to the resulting URL, use the [PT] flag as described below.

This doesn't seem to apply to your (very reasonable question), but if you dig further into [PT] :

The target (or substitution string) in the RewriteRule is assumed to be the default file path. Using the [PT] flag causes it to be treated as a URI.

For example (if you include LogLevel debug rewrite:trace6

):

RewriteRule ^/boo$ /opt/mock.png

      



Will be recorded:

[...] init rewrite engine with requested uri /boo
[...] applying pattern '^/boo$' to uri '/boo'
[...] rewrite '/boo' -> '/opt/mock.png'
[...] local path result: /opt/mock.png
[...] go-ahead with /opt/mock.png [OK]

      

but

RewriteRule ^/boo$ /opt/mock.png [PT]

      

Will be recorded:

[...] init rewrite engine with requested uri /boo
[...] applying pattern '^/boo$' to uri '/boo'
[...] rewrite '/boo' -> '/opt/mock.png'
[...] forcing '/opt/mock.png' to get passed through to next API URI-to-filename handler

      

+3


source







All Articles