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

Asked

Viewed 701 times

3

My directory is as follows:

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

I wish that when the user typed meusite.com/about, HTACCESS redirects you to the file about.php. However, if the user type meusite.com/about/, HTACCESS must use the folder, accessing the files contained in that directory.

Would be the trailing Slash which will define whether the URL will go to a directory or a file.

It is possible to do this?


EDIT 2

I managed to make the Urls standardized - that is, with a bar always at the end of them - and, at the same time, return the correct page, removing the extension .php.

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 manually. But I’m still looking for a better result.

  • is possible, just put the condition without the bar in . htaccess for PHP. It would be easier for you to say what you tried and explain what went wrong, because the solution you’re looking for is just like any other, the fact that it’s a bar or an entirely different name doesn’t change much.

  • Now, it’s no longer easy to save about.php as about/index.php simply, and leave everything in a bar?

  • @Bacco But then the site would be with Urls that have the Slash trailing and others, no. For example: meusite.com/about/ and meusite.com/about/brand. In my view, in this case, HTACCESS would have to add the missing Slash trailing, correct?

  • As for varying the bar at the end, you’re right. About adding the bar, this is very simple, just a rule that tests if it is directory and has bar at the end. It is not complicated what you want, but if [Edit] the question and put one . htaccess starting point helps, because in addition to being able to start from something within what you master, or have a notion, it’s easier to adjust than to make one from scratch, which can conflict with something else you want. Also, it is good to mention other situations. For example, what is to happen if one accesses /about.php instead of just /about ? It has these details.

  • Oops, it’s improved a lot with Edit. It looks like it’s on the right track, probably just needs a few tweaks. I don’t know if it would help to exchange the third Rule for RewriteRule ^(.*)$ $1.php [L], and I think you can make a few things simple.

  • Unless I just leave a rule to add the Slash trailing and then edit by RewriteRule ^about/brand/$ /about/brand.php [NC,L]

  • If no one responds and leaves some time later, I’ll take a slow read and see if I can see where to improve (I don’t promise, because I’m kind of wrapped up with some things here), but if I take a quiet in the pendencias here I give a thought on what you posted (Then I can do some tests, easier than just trying to imagine).

  • Look, @Bacco. I think I can handle it myself. The drug is that I’ll have to add the paths manually to HTACCESS. I’ll leave what I did to EDIT 2.

  • You can do "automatic" on that line of yours to test if it exists with. php at the end, it’s probably some simple adjustment (or delete some rule that’s getting in the way, at least for testing)

Show 4 more comments

1 answer

3


Try to see it that way:

# 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]
  • 2

    That code worked. Thank you, Cernuno.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.