Need Help To Protect my MVC PHP app folder using htaccess - php

I have an issue with my .htaccess.
Let's say that my app folder are:
/APP/
/CONTENT/
/UPLOADS/
/ASSETS/
And I have a .htaccess file that point all requests to my index.php, and the index php there is an included file called router.php that manage my roots:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([\s\S]*)$ index.php?url=$0 [QSA,L]
What i would want to do right now is to point APP and CONTENT folder also to the .htaccess and like that, i can display my custom 404 error page.

Related

MVC public folder htaccess is not working

I recently moved my index.php (the file that handles routing) and CSS, JavaScript, and font assets to a public/ folder. I only want items in this public/ folder to be accessible for security purposes. I ran into this problem when I realized I could visit mysite.com/composer.lock and view the composer lock file with my old .htaccess and folder setup.
Here is what I want
If I visit mysite.com/car/create, I want it to actually point to mysite.com/public/index.php?path=car/create
If I link to an asset such as mysite.com/css/style.css, I want it to really point to mysite.com/public/css/style.css
Here is my folder structure
Here is my .htaccess which is not working at all
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ public/index.php?path=$1 [L,QSA]
</IfModule>
How can I fix this? It just generates empty pages and I can still directly visit files in the root directory, etc.
Your existing directives specifically avoid rewriting requests for existing files, so it would still enable you to visit files in the root directory. It will also rewrite static resources to public/index.php?path=, which will presumably fail.
Try the following instead:
RewriteEngine On
# Stop processing if already in the /public directory
RewriteRule ^public/ - [L]
# Static resources if they exist
RewriteCond %{DOCUMENT_ROOT}/public/$1 -f
RewriteRule (.+) public/$1 [L]
# Route all other requests
RewriteRule (.*) public/index.php?path=$1 [L,QSA]

Can't access .xlsx file in a subdirectory

Here's my problem:
I have wordpress installed in the root folder
I created a folder named demo, also in the root folder
Now, this setup works, I can access php files inside the demo folder just fine (e.g. http://example.com/demo/test.php) as well as text files
The problem is when I try to access .xlsx files inside thedemo` folder the 404 page shows up
I'm not really sure if the problem is the .htaccess but here it is:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
I believe the best secure way to put files in wp-content to be able to easily access them

How to run a php file from root directory in xampp upon loading?

I want to load the draft.php file when i type the address in browser: localhost:81/mvcdemo .I have added this code in my .htaccess file:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ draft.php?url=$1 [QSA,L]
I have also changed Apache(httpd.config) file to get the rewrite on. So, what am i doing wrong?
You have conditions defined so that your RewriteRule will only be triggered, if the requested file, directory or link does not exist.
So if your .htaccess file is inside the directory mvcdemo and you're requesting said directory, the rule will not be triggered, because the directory exists.
Requesting http://localhost:81/mvcdemo/foo should trigger the rewrite rule, as it doesn't exist.
If you want all requests to the directory to be rewritten, then you can just do away with the conditions:
RewriteEngine On
RewriteRule ^(.*)$ draft.php?url=$1 [QSA,L]
Note that RewriteBase / sets the base URL as the document root, and since draft.php seems to be in the mvcdemo directory itself, you should do away with, as it will result in a draft.php not found error.
You could change apache's DirectoryIndex directive in your config file (or via an .htaccess) and append draft.php. Then it's auto-loaded like the usual index.html or index.php files.

Redirect all requests to a directory and it's subdirectories to a file inside the directory, using Apache2 server

The Document folders are structured like this
/var/wwww/ (root)
java/
...
php/
projectone/
controller/
view/
index.php
...
projecttwo/
...
I want all requests to /php/projectone or it's subfolders to redirect to /php/projectone/view/index.php, while requests to /php/projecttwo and other folders should not be redirected.
I believe using mod_rewrite is the way, however, I still haven't achieved what I want.
Place an .htaccess inside project one folder with the following rule:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /php/projectone/
RewriteRule ^(view/)?index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . view/index.php [L]
And you can repeat that for any other project folders you have. by changing the RewriteBase.

htaccess block access to directory but allow access to files

I have such situation.
I'm developing application in Zend Framework and htaccess pointing every request to
index.php. If some file or directory exists on the request path then htaccess allow access to this files like css, js, images etc...
Now I have for example such link:
example.com/profile/martin-slicker-i231
With Zend Router it points to controller account and action viewprofile. I want to add some avatart for my users and I created directory in public folder (so the images would be accessible by the server like css and js). The directory is named "profile" and subdirectory in it is "martin-slicker-i231". All path is visible by server like that:
public/
.htaccess
index.php
profile/
martin-slicker-i231/
avatar-small.jpg
The problem is when i point browser to example.com/profile/martin-slicker-i231 it
points me to this directory not the controller and action. When I remove the folder with user then the flow go to the controller and action. How to configure .htaccess so the example.com/profile/martin-slicker-i231 will be pointing to controller and action but request to example.com/profile/martin-slicker-i231/avatar-small.jpg point to the file. Here is my .htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
Can somebody help?
Just remove the portion that says that it should serve directories:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
Now it will serve files (with size > 0) and symbolic links directly, but send directory references and other urls to your application
According to the Apache docs the following will turn off directory listings
<Directory /path/to/directory>
Options -Indexes
</Directory>
You might also want to look at the documentation for mod_autoindex

Categories