Home > Blockchain >  How show contents if the folder does not contain index.php/.html using .htaccess?
How show contents if the folder does not contain index.php/.html using .htaccess?

Time:11-14

My set of folders looks like this:

server
--public
----index.php
----/items/
------index.php
------template.php
------/folder1/
------/folder2/
------/folder3/
--------index.php
------/folder4/

When this happens, folder3 in the browser shows the content at site.com/items/folder3/.

My task is to display in /folder1/, /folder2/, /folder4/ the content from the file site.com/items/template.php (because these folders do not contain index.php or index.html). How can I do this?

===========================

I tried this way, but it has disadvantages - These rules also work with non-existent directories (as /items/aaa/bbb/zzz/), which is inappropriate for me.

DirectoryIndex index.html index.htm index.php
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/$1/index\.html !-f
RewriteCond %{DOCUMENT_ROOT}/$1/index\.htm !-f
RewriteCond %{DOCUMENT_ROOT}/$1/index\.php !-f
RewriteRule ^(. ?)(?:/[^/] )?/?$ template.php [L]

CodePudding user response:

You can find your answer here https://stackoverflow.com/a/22412283/2118611

You should rewrite your empty folders to template.php

Try something like this:

# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# Rewrite all other URLs to template.php
RewriteRule .* /items/template.php [L]

EDIT:

To check if index file exist you can try something like this:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_FILENAME}/index\.php !-f
RewriteRule . /template.php [L]

The above code RewriteCond %{REQUEST_FILENAME} !-f check if the requested uri is not a file.

RewriteCond %{REQUEST_FILENAME} -d check if the requested uri is a directory.

RewriteCond %{REQUEST_FILENAME}/index\.php !-f check if the index.php is not found.

Then redirect to /template.php

  • Related