1

Simple question really.

After an internal redirect, the original 'context' is gone. I'd like to know if there is a variable or flag that allows me to check against the original request instead of the rewritten request. Pseudo code:

 RewriteCond %{REQUEST_FILENAME} !-f RewriteRule .+ public/$0.html [L] RewriteCond %{WHAT_USER_TYPED} ^public/(.+)\.html RewriteRule .+ - [R=404,L] 

In this example, when I type mywebsite.com/slug the request gets rewritten to path public/slug.html. Now, an internal redirect occurs and my path might match the second condition public/(.+)\.html, and returns a 404. That's not what I want. So, I'd like to know if there is some kind of solution to this.

1 Answer 1

1

Yet another reason why you should avoid adding rewriterules in .htaccess files. If you would do this in the main server context you would not have this problem. In a directory context or .htaccess file each rewrite causes an internal subrequest.

There is a parameter that contains the original request, It's "%{THE_REQUEST}

RewriteCond %{REQUEST_FILENAME} !-f RewriteRule (.*) public/$0.html?redir [L] RewriteCond %{THE_REQUEST} GET\ /public RewriteRule .* - [R=404,L] 

This should do it.

2
  • Thanks, that solves it! Good tip on the difference between htaccess files and server config. I chose .htaccess since this problem was on a shared hosting server, but I may want to reconsider. Commented Apr 9, 2013 at 10:48
  • "each rewrite causes an internal subrequest" - This isn't strictly a "subrequest". The IS_SUBREQ server variable remains "false" and rules with NS (nosubreq) still trigger. It's still the same request, it's just that after a redirect/rewrite the "request process starts over". Commented Nov 18, 2016 at 17:50

You must log in to answer this question.