A
A
Alexey Konovalov2021-04-19 15:06:46
htaccess
Alexey Konovalov, 2021-04-19 15:06:46

How to access files in a folder of the same name?

I have an admin folder and routing works in it. I want to make a request like https://site.ru/admin/method , but I keep getting an error because php is accessing the folder and not my file.
Here is the code I wrote in htaccess:

RewriteRule ^admin/(.*)$ /admin/run.php?request=$1 [QSA]

Tell me how to make php not try to access the folder, but access the run.php file?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
dodo512, 2021-04-19
@Alk90

How mod_rewrite actually works
It's important to understand that changing a query doesn't end with the last RewriteRule. After the last RewriteRule has fired, mod_rewrite looks to see if the request has changed or not. If the request has changed, its processing starts again from the beginning of .htaccess.
The address admin/run.phpsuccessfully matches the pattern ^admin/(.*)$and an infinite loop occurs.
After the LimitInternalRecursion is exhausted, the request processing fails with a 500 error. It is
enough to add an exception foradmin/run.php

RewriteCond %{REQUEST_URI} !^/admin/run\.php
RewriteRule ^admin/(.*)$ /admin/run.php?request=$1 [QSA,L]

Or so
RewriteCond $1 !^run\.php
RewriteRule ^admin/(.*)$ /admin/run.php?request=$1 [QSA,L]

Or add a check that the address is not an existing file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^admin/(.*)$ /admin/run.php?request=$1 [QSA,L]

The %{ENV:REDIRECT_STATUS} variable stores the code with which the previous internal redirect ended. This variable is empty only on the first iteration of request processing.
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^admin/(.*)$ /admin/run.php?request=$1 [QSA,L]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question