14

How do I remove the file type from my webpages without creating a new directory and naming the file index.php. I want http://example.com/google.html to http://example.com/google.

How would I go about doing this.

PS: I tried looking at some other tutorials but there to confusing. I do now that it can be done in .htaccess

Arman P.
  • 4,314
  • 2
  • 29
  • 47
SimplePi
  • 95
  • 1
  • 4
  • 15
  • @MarcB Yes, Im aware. However i tryed that and it dosent work. Plus i need to rename all my files. – SimplePi Feb 07 '14 at 01:16
  • If you want to learn how to remove `php` and `html` extensions from URLs using htaccess , You can try this link https://helponnet.com/2020/02/04/remove-html-and-php-extension-with-htaccess-rewriterule-url-rewriting-tips/ – Amit Verma Jun 17 '21 at 07:05

3 Answers3

23

Yes, I know that this question was asked multiple times already and is answered, but I will give a little more comprehensive answer based on my experience.

Here is the .htaccess code snippet that will help you:

# Apache Rewrite Rules
 <IfModule mod_rewrite.c>
  Options +FollowSymLinks
  RewriteEngine On
  RewriteBase /

# Add trailing slash to url
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/|#(.*))$
  RewriteRule ^(.*)$ $1/ [R=301,L]

# Remove .php-extension from url
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME}\.php -f
  RewriteRule ^([^\.]+)/$ $1.php 

# End of Apache Rewrite Rules
 </IfModule>

I want to stress some important things here for everybody's reference:

  • This code snippet doesn't remove entry scripts from url (such as index.php used by many PHP frameworks)
  • It only removes .php extension, if you want to remove other extension as well (e.g. .html), copy and paste 3rd block and replace php with other extension.
  • Don't forget to also remove extension from anchors (links) href.
Arman P.
  • 4,314
  • 2
  • 29
  • 47
  • 1
    @SimplePi Than I will suggest you accept the answer and mark it, so that this question doesn't show up in unanswered ones. If you have any problems, let me know, so that I could help you further with this. – Arman P. Feb 07 '14 at 01:28
  • 1
    Yep, Just had to wait a few minutes before i accepted it due to restrictions. – SimplePi Feb 07 '14 at 01:29
16

@armanP's accepted answer above does not remove .php extension from php urls. It just makes it possible to access php files without using .php at the end. For example /file.php can be accessed using /file or /file.php but this way you have 2 diffrent urls pointing to the same location.

If you want to remove .php completely, you can use the following rules in /.htaccess :

RewriteEngine on 
#redirect /file.php to /file
RewriteCond %{THE_REQUEST} \s/([^.]+)\.php [NC]
RewriteRule ^ /%1 [NE,L,R]
# now we will internally map /file to /file.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)/?$ /$1.php  [L]

To remove .html ,use this

 RewriteEngine on
 #redirect /file.html to /file
 RewriteCond %{THE_REQUEST} \s/([^.]+)\.html [NC]
 RewriteRule ^ /%1 [NE,L,R]
 # now we will internally map /file to/ file.html
 RewriteCond %{REQUEST_FILENAME}.html -f
 RewriteRule ^(.*)/?$ /$1.html  [L]

Solution for Apache 2.4* users :

If your apache version is 2.4 ,you can use the Code without RewriteConditions On Apache 2.4 we can use END flag instead of the RewriteCond to prevent Infinite loop error.

Here is a solution for Apache 2.4 users

 RewriteEngine on

 #redirect  /file.php to /file
  RewriteRule ^(.+).php$ /$1 [L,R]
 # now we will internally map /file to /file.php
 RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)/?$ /$1.php [END]

Note : If your external stylesheet or images aren't loading after adding these rules ,to fix this you can either make your links absolute changing <img src="foo.png> to <img src="/foo.png> .Notice the / before the filename . or change the URI base add the following to head section of your Web page <base href="/"> .

Your Webpage fails to load css and js due to the following reason :

When your browser url changes from /file.php to /file server thinks that /file is a directory and it tries to append that in front of all relative paths . For example : when your url is http://example.com/file/ your relative path changes to <img src "/file/foo.png"> thus the image fails to load . You can use one of the solutions I mentioned in the last peragraph to solve this issue.

Amit Verma
  • 40,709
  • 21
  • 93
  • 115
  • 1
    Thanks Amit, your answer is solved my problem. I'm using NuxtJS and somehow htaccess doesn't work I expected. But your answer is best solution to remove php and html extensions from url. So my router is working perfectly now. Thanks so much. – Yasin Yörük Oct 30 '20 at 08:05
  • Note that this seems to conflict with "ErrorDocument 404 /404.php" which is commonly used to set a custom 404 page. The problem is that the 404 page now returns a 200 status code which is interpreted as a soft 404. To fix this, you need to add "RewriteCond %{REQUEST_URI} !^/404" as the first rewrite condition before both rewrite rules here so that neither of these rules will run for the 404 page. This solves the issue I described above. – train Aug 27 '22 at 01:36
0

Here is the fix for the internal path when removing .php (using code above as a reference).

# Start of Apache Rewrite Rules

 <IfModule mod_rewrite.c>
 
  Options +FollowSymLinks
  RewriteEngine On
  RewriteBase
  
# From http:// to https://
  
  RewriteCond %{ENV:HTTPS} !=on
  RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
  
# Redirect /file.php to /file with POST (enabled)

  RewriteCond %{REQUEST_METHOD} !POST     
  RewriteCond %{THE_REQUEST} \s/([^.]+)\.php [NC]
  RewriteRule ^ /%1 [NE,L,R]

# Add trailing slash to url

  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/|#(.*))$
  RewriteRule ^(.*)$ $1/ [L]

# Remove .php-extension from url

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

 </IfModule>

# End of Apache Rewrite Rules

Note:

Above is the code for accepting URL without .php, URL with .php will be transformed without .php (.php will be removed) all files/folders with their respective path will open normally without any issue, also post & get method will work just fine.

FSK
  • 3
  • 3