Htaccess Rewrite Rules and Conditions

Viewed 28

I have started yet another little project for personal uses, and I am setting up my web root htaccess file. I haven't touched this stuff for too long so I am having some issues with it.

I will show some examples of what I want to do because I don't know how to explain it well. My webroot contains:

/index.php

/test/test.txt

If the url is example.com/home, it should output example.com/index.php?page=home. That is pretty easy to do and I don't have any issue with it. The issue starts with the existing files. If the url is example.com/test or example.com/test/test.txt, I want it to output example.com/index.php?file=test or example.com/index.php?file=test/test.txt These are the conditions and rules in my htaccess file (these are meant for the existing files only):

RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ - [S=1]
RewriteRule ^(.*?)$ /index.php?file=$1 [L,QSA]

However, when I enter example.com/test, it goes straight to the directory "test" and the same with example.com/test.txt - just displays the file.

First of all, is it alright to do this? And if so, how can I achieve this? Thanks in advance!

P.S. How do I log the processing of my rewrite rules? I found that I can do this by modifying LogLevel in httpd.conf. I had it set to LogLevel alert rewrite:trace6, but the errorlog doesn't seem to contain the logs I need.

1 Answers

You have asked multiple questions, so this is a partial answer.

Preventing Directory Listing

For any folder that does not have index.php/html, if you do not want the contents listed, create a .htaccess file in that folder with the contents Options -Indexes

Preventing File Execution

For any folder that has files for reference (not meant to be executed or shown), change the permission to 750 They can still be included or referenced server side.

Related