Question

I have a file and folder with the same name with dropping file extensions

Posted April 5, 2021 139 views
Apache

I know that I can fix my .htaccess I just dont have any knowledge on .htaccess:

DirectorySlash Off
Options +FollowSymLinks -MultiViews -Indexes
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.html [NC]
RewriteRule ^ %1 [R,L,NC]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^ %{REQUEST_URI}.html [L]

ErrorDocument 404 /404.html
ErrorDocument 403 /403.html

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
Submit an Answer
1 answer

Hi @Filice,

I’m not sure I understand the question exactly. What I understand is that you wish your website to load without using the file’s extensions. For example, if you have a domain.com and someone loads the contact.php form to look like
domain.com/contact, is that correct?

Removing Extensions

To remove the .php extension from a PHP file for example yoursite.com/wallpaper.php to yoursite.com/wallpaper you have to add the following code inside the .htaccess file:

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

If you want to remove the .html extension from a html file for example yoursite.com/wallpaper.html to yoursite.com/wallpaper you simply have to change the last line from the code above, to match the filename:

RewriteRule ^([^\.]+)$ $1.html [NC,L]

Regards,
KFSys

  • The problem that Im facing right now is that when I make a folder with the same name as a file like folder: images file: images.html, It will just go to the index of that directory, or for me, I made it so it goes to 403 because I disabled indexes. I have it so it gets rid of file extensions.

    • Hi @Filice,

      Did you try replacing your .htaccess code with what my suggestion above, did it work or it got you to the above issue you are describing?

      • I am not trying to get rid of file extensions as I already have that in my .htaccess. What Im trying to elucidate is that when making a folder with the same name as a html file, it goes to the folders index.

        file: "example.html"
        folder: "example"
        Going to "www.domain.com/example" redirects to the folders index.
        

        You were talking about getting rid of file extensions but I already have that, its working but the problem is, is when going to the www.domain.com/example it redirects to the index of example folder instead of the example html file. Having no extensions causes this problem as they both have the same name.