Use .htaccess in a directory with files and folders with the same name

3

My directory is as follows:

root/
├── index.php
├── about.php
├── privacy.php
└── about/
    ├── brand.php
    └── history.php

I would like that when the user types meusite.com/about , HTACCESS redirects it to the about.php file. However, if the user types meusite.com/about/ , HTACCESS should use the folder by accessing the files contained in that directory.

It would be the trailing slash that will define whether the URL will go to a directory or a file.

Is it possible to do this?

EDIT 2

I was able to make the URLs default - that is, with a slash always at the end of them - and at the same time, return the correct page by removing the .php extension.

RewriteEngine on
RewriteBase /

RewriteCond %{REQUEST_URI} !(/$|\.)
RewriteRule (.*) %{REQUEST_URI}/ [R=301]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\ (.*)\.php [NC]
RewriteRule ^ %1 [R=301]

RewriteRule ^about/brand/$ /about/brand.php [NC,L]

The drug is having to add everything manual. But I'm still looking for a better result.

    
asked by anonymous 23.01.2017 / 20:06

1 answer

2

Try to look like this:

# Reescreve as solicitações .php originais em novas URLs
RewriteCond %{THE_REQUEST} \ /([^.]+)\.php [NC]
RewriteRule ^ /%1/ [R,L]

# Força a adição da trailing slash
RewriteCond %{REQUEST_URI} !\..{3,4}$
RewriteRule ^(.*)([^/])$ http://%{HTTP_HOST}/$1$2/ [L,R=301]

# Redireciona para .PHP se não existe diretório
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ $1.php [L]
    
23.01.2017 / 21:27