htaccess directory management

Viewed 94

im going to ask a really simple question. i dont want my link to show this when i run my page :

http://localhost/example/assets/gallery.php

what i want is :

http://localhost/example/assets/

so how to do it in .htaccess file ?

i would really appreciate it if you can help because im so confused after reading forums .

my htaccess is like this right now but you know it only helps to remove extension :

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

How to make assets/gallery.php -> assets/


  1. In the assets folder make a .htaccess file

  2. Paste in this code :

    DirectoryIndex gallery.php
    

This code changes the Directory Index (like a index.php file) to the gallery.php file meaning gallery.php is now like the index.php file.

The DirectoryIndex method that @RyanTheGhost suggests in his answer should have worked for the specific example you posted (where you request a directory and are serving a file from within that directory). However, the mod_rewrite directives you currently have in the document root1 will conflict with any requests for directories2 (although the DirectoryIndex should take priority).

However, the DirectoryIndex method is not very practical if you have many such files. And if you are not requesting a directory then this method naturally won't work anyway.

You could instead rewrite the URL using mod_rewrite in your existing .htaccess file, before your current directives.

1 I'm assuming your .htaccess file is in the document root.

For example:

# Rewrite "/example/assets/" to "/example/assets/gallery.php"
RewriteRule ^example/assets/$ example/assets/gallery.php [L]

Or, to avoid repitition:

# Rewrite "/example/assets/" to "/example/assets/gallery.php"
RewriteRule ^example/assets/$ $0gallery.php [L]

Where the $0 backreference contains the entire URL-path that is matched by the RewriteRule pattern. ie. example/assets/ in this case. NB: There is no slash prefix on the RewriteRule pattern or the substitution string.

Note that since you are requesting a directory (ie. /example/assets/) you need to ensure there is no DirectoryIndex document in that directory (eg. index.html or index.php), otherwise this will be served (by mod_dir) instead, overriding your internal rewrite above.


2 Your current directives that append the .php extension are arguably incorrect:

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

This rule appends the .php extension to any request that does not map to a physical file, even if the file with a .php extension does not exist either. This can result in the incorrect URL being reported back to the user in the 404 error document (depending on how this is implemented). For example, the default Apache 404 error document will report that /foo.php does not exist, when the user requested /foo.

This rule will also rewrite directories (since they are "not files") which will result in a 404 (as opposed to a 403 or directory listing, if enabled). Although a DirectoryIndex document will override this.

Additionally, the NC flag is superfluous and there is no need to backslash-escape the literal dot inside a character class.

You could instead check that the corresponding .php file exists before rewriting, instead of checking that the requested URL does not map to a file.

For example:

RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([^.]+)$ $1.php [L]

The request is now only rewritten when the corresponding .php file exists, which naturally avoids any conflicts with directories.

Related