How to give access execute specific php file in server?

917 views Asked by At

Hello guys first of all sorry for my bad english!. I'm having trouble with running a specific php file in server and I want to configure .htaccess to allow user to run the file. example: www.mywebsite.com/theme/test.php

this is my .htaccess file and when I check my site it return "404 Not Found" `

<IfModule mod_negotiation.c>
    Options -MultiViews
</IfModule>

RewriteEngine on

##
## You may need to uncomment the following line for some hosting environments,
## if you have installed to a subdirectory, enter the name here also.
##
# RewriteBase /

##
## Black listed folders
##
RewriteRule ^bootstrap/.* index.php [L,NC]
RewriteRule ^config/.* index.php [L,NC]
RewriteRule ^vendor/.* index.php [L,NC]
RewriteRule ^storage/cms/.* index.php [L,NC]
RewriteRule ^storage/logs/.* index.php [L,NC]
RewriteRule ^storage/framework/.* index.php [L,NC]
RewriteRule ^storage/temp/protected/.* index.php [L,NC]
RewriteRule ^storage/app/uploads/protected/.* index.php [L,NC]

##
## White listed folders
##
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} !/.well-known/*
RewriteCond %{REQUEST_FILENAME} !/storage/app/uploads/.*
RewriteCond %{REQUEST_FILENAME} !/storage/app/media/.*
RewriteCond %{REQUEST_FILENAME} !/storage/temp/public/.*
RewriteCond %{REQUEST_FILENAME} !/themes/.*/(assets|resources)/.*
RewriteCond %{REQUEST_FILENAME} !/plugins/.*/(assets|resources)/.*
RewriteCond %{REQUEST_FILENAME} !/modules/.*/(assets|resources)/.*
RewriteRule !^index.php index.php [L,NC]


##
## Block all PHP files, except index
##
 RewriteCond %{REQUEST_FILENAME} -f
 RewriteCond %{REQUEST_FILENAME} \.php$
 RewriteRule !^index.php index.php  [L,NC]

##
## Standard routes
##

 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteRule ^ index.php [L]

` Is there any way to fix the problem ?

2

There are 2 answers

3
elixenide On BEST ANSWER

Your problem is that the rule labeled "Block all PHP files, except index" will route all requests for PHP files to the index file. Here are two ways to fix this.

Simple, One-Off Solution

You can simply add this rule before any other RewriteRule lines:

RewriteRule ^theme/test\.php - [L,NC]

More Flexible Solution

The idea below takes a different approach and works with your existing whitelisting rules. This might be better if you are goi to have multiple patterns or something more complex than a single file to worry about.

For your "white listed folders" rule, add the condition

RewriteCond %{REQUEST_FILENAME} !/theme/test\.php

So the block becomes

##
## White listed folders
##
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} !/.well-known/*
RewriteCond %{REQUEST_FILENAME} !/storage/app/uploads/.*
RewriteCond %{REQUEST_FILENAME} !/storage/app/media/.*
RewriteCond %{REQUEST_FILENAME} !/storage/temp/public/.*
RewriteCond %{REQUEST_FILENAME} !/theme/test\.php
RewriteCond %{REQUEST_FILENAME} !/themes/.*/(assets|resources)/.*
RewriteCond %{REQUEST_FILENAME} !/plugins/.*/(assets|resources)/.*
RewriteCond %{REQUEST_FILENAME} !/modules/.*/(assets|resources)/.*
RewriteRule !^index.php index.php [L,NC]

Otherwise, the rule labeled "Block all PHP files, except index" will route to the index file.

2
Unknown Kadaba On

Bro, maybe you can try with this:

RewriteRule /Route/to/Your.php

Your problem happens because you are denying all the files .php

Lucky with that.