1

I am using this snippet for enabling clean URLs, effectively removing the PHP extension.

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

## hide .php extension snippet

# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]

# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]

This works pretty well, but the problem is that I have some directories that share the same name as some of the PHP files.

So instead of the desired page, I get the directory structure.

I tried adding DirectorySlash Off before all of the rewrite rules, but no luck with that.

I've read that I must remove the RewriteCond %{REQUEST_FILENAME} !-d if I am using DirectorySlash Off, that didn't work also.

Not to mention I tried around dozen various snippets when looking for a solution.

What is the proper way to handle my scenario?

Paul Dixon
  • 295,876
  • 54
  • 310
  • 348
fluxus
  • 559
  • 5
  • 15
  • 29
  • Just to note if u change `-MultiViews` to `+MultiViews` you don't need all the code you wrote.. it does it automatically and not only with php but with css and js, png, etc.. – Konstantinos Mar 30 '18 at 18:46
  • @Konstantinos Thanks for the tip. But unfortunately the problem remains. – fluxus Mar 30 '18 at 18:49
  • Yes it does i just mentioned that you don't need all that code to do what could be done with 1 character change. Now for your other problem what do you want to prioritize? first check if file exist and load it or first check if folder exist and load it? What do you want to be loaded first if they have same name. – Konstantinos Mar 30 '18 at 18:52
  • I want files to load, so if they both share the same name, prioritise files. – fluxus Mar 30 '18 at 19:07
  • Need some examples. e.g. if request is `example.com/abc/` and there is `/abc.php` as well a directory called `/abc/` then you want to load `/abc.php` ? – anubhava Mar 30 '18 at 19:19
  • @anubhava Exactly like you said it. – fluxus Mar 30 '18 at 19:23
  • Honestly, if I were you I'd rather avoid all this rewriting in such a case, and rename `/abc.php` in `/abc/index.php` and be done with it ... – CBroe Mar 30 '18 at 19:40
  • That is the last resort, I know. But I've spent around four hours trying to solve this, I will wait a bit more for a potential solution. – fluxus Mar 30 '18 at 19:45

1 Answers1

1

Change your .htaccess to this:

Options +FollowSymLinks -MultiViews -Indexes
RewriteEngine On
RewriteBase /

## hide .php extension snippet

# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,NE,L]

RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
  • Keep DirectorySlash turned on as the default value for security reasons.
  • Options -Indexes is used for disabling directory listing.
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • Sorry about that. All URL's with .php are still displayed like that. If I remove the .php I get 404. – fluxus Mar 30 '18 at 19:47
  • I entered `example.com/abc.php` . First I would like to have it without `.php`, so it would read `example.com/abc` . More importantly, if I enter `example.com/abc.php` and there is a directory named `example.com/abc` I want to load `example.com/abc.php` – fluxus Mar 30 '18 at 19:56
  • The remove/rewrite part is done correctly with the initial snippet(taken from one of your SO answers). The problem arises because some of the files are named the same as directories. Thus making it impossible to load the file, instead loading the directory. – fluxus Mar 30 '18 at 19:58
  • Because after removing the `.php` from the file, it has exact same name as the directory. – fluxus Mar 30 '18 at 19:59
  • ok I suspect bad browser cache or other rewrite rules or another .htaccess as problem because I used exactly this code on my local Apache. I have a `/page.php` and a directory with name `/page/`. When I opened `localhost/page.php` it got redirected to `localhost/page/` and it loaded `/page.php` internally. – anubhava Mar 30 '18 at 20:02
  • That is the behaviour I want. Could it be that the site in question is actually a subdomain? – fluxus Mar 30 '18 at 20:04
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/167899/discussion-between-anubhava-and-suludi). – anubhava Mar 30 '18 at 20:05