Pergunta

Quick question for you guys. My website's basic file structure is as follows:

www.example.com

/

index.php

otherFiles.php

./directory

   index.php

Currently if you were to type in example.com/directory it would redirect to my 404 page. You have to type in example.com/directory/index. My assumption was that for every directory, the default page was index with either the .php or .html extension. I guess I was wrong, so I configured that directory's .htaccess as follows:

DirectoryIndex index.php

But, no progress. Is there something I am missing? To clarify, I would like the url of example.com/directory to open the index file in that directory. Here is my universal .htaccess:

ErrorDocument 404 http://www.example.com/404

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

Edit

So, I commented out the 3 Rewrite commands in my htaccess and now my issue is resolved. The problem is that now the php file extensions are showing. Is there a way to include rewrite rules without interfering with default index pages? Or is the problem somewhere else?

Foi útil?

Solução

It looks like the 404 is due to the directory /directory.php not existing. Because your RewriteRule is executed with the condition only that it isn't a real file (%{REQUEST_FILENAME} !-f), the rule is mistakenly applied for directories, attempting to append .php. It's easily fixed with a !-d:

ErrorDocument 404 http://www.example.com/404

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L]

Now, when a directory like /directory is requested, the default DirectoryIndex will be used and no rewrite will take place.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top