1

This is probably a real easy question. I'm just having a simple clean URL kind of setup, so www.website.com/articles/music would rewrite it to index.php?cat=articles&subcat=music or whatever, that's just an example. But of course if it's a file request (e.g. website.com/images/icons/music.png) it'd check for a valid file first. That was easy enough, there's plenty of tutorials online for that using .htaccess and rewrite rules which gave me something like this

RewriteEngine On
# Check for existing files first
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Set base
RewriteBase /
# Rewrite rules not shown but would be below

The last condition I want to make is it also checks if the given URL folderpath exists and there is an index.php or index.html in it. E.g. continuing the first example, check to make sure there isn't a website.com/articles/music/index.html or index.php either, and if so, redirect to that instead of the rewrite rules.

Like I said probably simple enough, but I'm not good with htaccess stuff since I rarely use it and it was an irregular question that I couldn't find answers for. Not that they probably don't exist but the wording I tried to use didn't pop up anything. Thanks.

anubhava
  • 761,203
  • 64
  • 569
  • 643
wowohweewah
  • 429
  • 5
  • 16

1 Answers1

2

You need to have RewriteCond to check for the presence of `index.html (or php) in the URI folder:

RewriteEngine On
# Set base
RewriteBase /

# Check for existing files first
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

# check for index.php
RewriteCond %{DOCUMENT_ROOT}/$1/index\.php -f [NC]
RewriteRule ^(.+?)/?$ $1/index.php [L]

# check for index.html
RewriteCond %{DOCUMENT_ROOT}/$1/index\.html -f [NC]
RewriteRule ^(.+?)/?$ $1/index.html [L]

# route to default /imdex.php
RewriteRule . index.php [L]
anubhava
  • 761,203
  • 64
  • 569
  • 643