Hi everyone!
I’m trying to convince web browsers to remove the .html and .php extension, but they don’t seem to be listening.
Current Code:
Right now, the site works fine, weather the URL has the extension or not. (removed.com - Informationen zum Thema removed. & removed.com - Informationen zum Thema removed. both work) However, if you were to go to removed.com - Informationen zum Thema removed. it will not redirect you to removed.com - Informationen zum Thema removed. . Does someone know if a line is preventing this from happening, or if I need to add something? Google searching has not turned up anything useful for me.
Thanks!!!
KangJL
March 26, 2021, 4:15pm
2
Use Options +MultiViews
to disable file extension
1 Like
Admin
March 26, 2021, 4:16pm
3
There is no line to do that in your screenshot. The first “remove php file extension” line only does the thing you described first:
If you want to also redirect Contact.html
to Contact
(if the file exists, of course), you’ll need additional .htaccess rules to do that.
2 Likes
HI, can you explain to be how that would be done? I am new to htaccess, and it kind of looks like giberish here
That’s weird, because it works!
Do you have any idea what these could be?
Thanks!
I can help you with that.
There’s a DreamHost article containing information about changing and removing file extensions (like the community guide i made where you can create extensions based off old ones)
From the DreamHost Article:
This example completely removes the file extension from your URL. So, example.php would appear as example. The following example is for .php files, but for any other type, just replace .php with your desired type and add those lines and extensions in the same way:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\ HTTP/
RewriteRule ^(.*)index$ http://example.com/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ http://example.com/$1 [L,R=301]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.+)\.php\ HTTP/
RewriteRule ^(.+)\.php$ http://example.com/$1 [L,R=301]
RewriteRule ^([a-z]+)$ /$1.php [L]
You can also force it without the extension too by clicking the DreamHost Article button so this reply doesn’t go long.
2 Likes
This could help you but I haven’t tested it out yet. (DreamHost is a true source)
alexvf
March 26, 2021, 5:23pm
8
I know that you can use this to remove the .php extension because it works for me:
Options +MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
I know that you can use this to remove the .html extension because it works for me on another website:
Options +MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
I don’t know how you can use both. If I had to do it, I would test it like this, but I never tried it and don’t have a clue if it works:
Options +MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^([^\.]+)$ $1.html [NC,L]
2 Likes
Thanks for the examples! I’ll check them out when I am free later and let you know how they work!
Edit: I can’t spell
The example @alexvf provided works.
1 Like
This exact code also works perfectly to remove .html and .php file extentions:
Options +MultiViews +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.html [NC]
RewriteRule ^ %1 [R,L,NC]
5 Likes
Thanks so much @TigerMANEK426 @alexvf and @duckyishot93 ! I wish I could mark all of your solutions as correct. Thanks for the code and explanations, it is much appreciated!
1 Like
system
Closed
April 13, 2021, 3:19pm
14
This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.