logo
Apache Lounge
Webmasters

 

About Forum Index Downloads Search Register Log in RSS X


Keep Server Online

If you find the Apache Lounge, the downloads and overall help useful, please express your satisfaction with a donation.

or

Bitcoin

A donation makes a contribution towards the costs, the time and effort that's going in this site and building.

Thank You! Steffen

Your donations will help to keep this site alive and well, and continuing building binaries. Apache Lounge is not sponsored.
Post new topic   Forum Index -> Apache View previous topic :: View next topic
Reply to topic   Topic: .htaccess -- This works but I'm struggling to understand why
Author
stardustvega



Joined: 28 Feb 2023
Posts: 14

PostPosted: Sun 02 Apr '23 23:42    Post subject: .htaccess -- This works but I'm struggling to understand why Reply with quote

I'm new to Apache and I feel like I'm missing something here.

I've got the following .htaccess, which does work (combined from some tutorials and a StackOverflow answer):

Code:
RewriteEngine On

RewriteRule ^public/ - [L,NC]

RewriteCond public/$0 -F
RewriteRule .* public/$0 [L,NC]

RewriteRule (.*) public/index.php [L,NC]


However, I'm struggling to understand exactly how the second rule actually works.

A file exists at the location http://[my root]/public/images/test-img.png. With the rewrite rules above, you can access it at http://[my root]/images/test-img.png. This *is* the expected behavior, I'm just struggling to understand how it works.

This is how I'm reading these rules, but I think I must not understand something at Step 3.

1) The user requests http://[my root]/images/test-img.png
2) It hits the first rewrite rule and checks if the path starts with 'public'. It doesn't, so it proceeds to the second rule.
3) It hits the second rewrite rule and checks if the path is within the public directory. The file requested *is* within the public directory, but since the address the user requested doesn't say that, I can't work out how htaccess knows that.
4) If the second rewrite rule didn't fire, it would pass the request over to public/index.php (which I think restarts htaccess from the start and then stops at the first rule).
Back to top
covener



Joined: 23 Nov 2008
Posts: 59

PostPosted: Mon 03 Apr '23 1:30    Post subject: Re: .htaccess -- This works but I'm struggling to understand Reply with quote

Quote:
but since the address the user requested doesn't say that



The -F at the end of the condition means to test that the path in the first argument exists, by sending a request through the server and seeing if it succeeds.

The $0 is everything matched by the rewriterule regex (which is .*, so the original requested URL with the path that lead to htaccess stripped off)

Since it's in .htaccess, there is a little extra magic that makes all the "relative to the current URL that lead to .htaccess" magic work. If they were all absolute filesystem paths in the directives, I think it would make more sense at a glance.

So to me the 2nd cond/rule says e.g.

"If the remainder of the URL that leads to this .htaccess also exists under the public/ directory, rewrite the URL to start with public/"
Back to top
stardustvega



Joined: 28 Feb 2023
Posts: 14

PostPosted: Mon 03 Apr '23 23:55    Post subject: Reply with quote

Thanks for the reply. I don't follow yet, but I need to sit down and write it through, I'll do that with your explanation and see if I understand better.

(Had something come up so I can't right away, but I didn't want to leave a helpful comment hanging without a response.)
Back to top
James Blond
Moderator


Joined: 19 Jan 2006
Posts: 7373
Location: Germany, Next to Hamburg

PostPosted: Sat 08 Apr '23 23:34    Post subject: Reply with quote

stardustvega wrote:
Thanks for the reply. I don't follow yet, but I need to sit down and write it through, I'll do that with your explanation and see if I understand better.


What part you don't understand?
Back to top
stardustvega



Joined: 28 Feb 2023
Posts: 14

PostPosted: Tue 11 Apr '23 0:31    Post subject: Reply with quote

Sorry for the delay.

Okay, let's see if I understand this.

So, .htaccess exists at my root. I think what you're saying is that the rewrite rule ignores anything 'before' that level, so it's only going to look at /images/test-img.png, because that's the part that's past the point where .htaccess lives.

So, the $0 in this case represents /images/test-img.png. The rewrite rule says to put 'public' in front of that so it becomes public/images/test-img.png.

The -F says 'Check if an asset really exists at that location'. Because it does, the rewrite rule proceeds. But if it didn't (e.g. if no asset existed at that location), it would just go on to the next rule.

Is that correct?
Back to top
James Blond
Moderator


Joined: 19 Jan 2006
Posts: 7373
Location: Germany, Next to Hamburg

PostPosted: Wed 19 Apr '23 8:38    Post subject: Reply with quote

The -F tells the server to do an actual http request to itself to see if the resource if available.
Back to top
stardustvega



Joined: 28 Feb 2023
Posts: 14

PostPosted: Wed 19 Apr '23 11:16    Post subject: Reply with quote

OK. The -F does an HTTP request to check if he resource is available. Is the rest of it conceptually correct?
Back to top
James Blond
Moderator


Joined: 19 Jan 2006
Posts: 7373
Location: Germany, Next to Hamburg

PostPosted: Wed 19 Apr '23 13:41    Post subject: Reply with quote

Yepp, you got the rest correct!
Back to top
stardustvega



Joined: 28 Feb 2023
Posts: 14

PostPosted: Wed 19 Apr '23 14:01    Post subject: Reply with quote

Awesome, thank you both for your help!
Back to top


Reply to topic   Topic: .htaccess -- This works but I'm struggling to understand why View previous topic :: View next topic
Post new topic   Forum Index -> Apache