Remove .php file extension with .htaccess file

I want www.example.com/about.php to just be www.example.com/about

I created a .htaccess file and put it in the root of my server. I use shared linux hosting. PHP version 5.2

RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}\.php -f RewriteRule ^(.*)$ $1.php 

It seems that this code should work correctly, but for some reason this is not the case. I get a 404 error when I try to go to a page without the .php extension.

Here is my markup too:

 <nav> <div class="container"> <ul> <li><a href="index.php" <?php if (strpos($_SERVER['PHP_SELF'], 'index.php')) echo 'class="current-nav"';?>>home</a></li> <li><a href="about.php">about</a></li> <li><a href="services">our services</a></li> <li><a href="portfolio" <?php if (strpos($_SERVER['PHP_SELF'], 'portfolio.php')) echo 'class="current-nav"';?>>portfolio</a></li> <li><a href="testimonials" <?php if (strpos($_SERVER['PHP_SELF'], 'testimonials.php')) echo 'class="current-nav"';?>>testimonials</a></li> <!--<li><a href="#">client area</a></li>--> <li><a href="contact" <?php if (strpos($_SERVER['PHP_SELF'], 'contact.php')) echo 'class="current-nav"';?>>Contact</a></li> <li><a href="order" <?php if (strpos($_SERVER['PHP_SELF'], 'order.php')) echo 'class="current-nav"';?>><strong>Free Quote</strong></a></li> </ul> </div> </nav><!--/navigation--> 

You can see that I tried using the php extension in the link, and also tried without it. Links with the php extension go to the page but do not remove the extension.

I did a test to find out if module_rewrite is enabled by inserting some garbage and returning a 500 error.

+7
source share
5 answers

Use this code to hide / remove .php extension:

 Options +FollowSymLinks -MultiViews # Turn mod_rewrite on RewriteEngine On RewriteBase / ## hide .php extension # To externally redirect /dir/foo.php to /dir/foo RewriteCond %{THE_REQUEST} ^[AZ]{3,}\s([^.]+)\.php [NC] RewriteRule ^ %1 [R,L,NC] ## To internally redirect /dir/foo to /dir/foo.php RewriteCond %{REQUEST_FILENAME}.php -f RewriteRule ^ %{REQUEST_URI}.php [L] 
+35
source

So, after a long battle with google, I finally figured it out. This works with the joint move of Godaddy. It removes the php file extensions, so http://yoursite.com/about.php becomes http://yoursite.com/about

 Options -MultiViews RewriteEngine On RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f #If requested is not a filename... RewriteCond %{REQUEST_FILENAME} !-d #And not a directory RewriteRule ^(.*)$ $1.php [L] # perform this redirect 

(delete comments before uploading to the server)

+7
source

I had this problem too, but I found that this seems to fix the GoDaddy.htaccess problem.

 # Fix Rewrite Options -Multiviews # Mod Rewrite Options +FollowSymLinks RewriteEngine On RewriteBase / RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}\.php -f RewriteRule ^(.*)$ $1.php 
+4
source

The above everything in my .htaccess file worked for me ...

 ## 301 Redirects RewriteCond %{QUERY_STRING} ^$ RewriteRule ^(.*)\.asp$ $1? [R=301,NE,NC,L] 
+1
source

The rewrite rule is incorrect; this is not true. Try instead:

 RewriteRule ^(.*).php$ /$1 [L,R=301] 
0
source

All Articles