Search code examples
.htaccesshttp-redirecturl-rewriting

How to redirect the Index of / to a website?


I made a VirtualHost to access to a php file and I want to allow access to this page and files in folder1. Here are my folders :

root
|mywebsite.php
|.htaccess
|otherfiles
|
|--images
|  |image.jgp
|
|--style
|  |style.css
|
|--folder1
|  |a_lot_of_folders

I can deny the access to the otherfiles in the root directory. If the URL is : www.ipaddress/ or www.ipaddress/otherfiles it will redirect to www.ipaddress/mywebsite.php. But what I want is allow access only to the php file and files in folder1. But if the URL looks like this : www.ipaddress/folder1 or this : www.ipaddress/style it will open the Index of /folder and I don't want this.

Here is my .htaccess :

RewriteEngine on

RewriteRule ^$ mywebsite.php
RewriteRule ^otherfiles$ mywebsite.php # This is for each files in the root folder except the php one
RewriteRule ^/folder1? mywebsite.php # This doesn't work

I tried to rewrite the URL when it has /folder1 but it does nothing


Solution

  • Try this as your last rules:

    RewriteRule ^folder1/?$ mywebsite.php [L,NC]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^folder1/ mywebsite.php [L,NC]