0

I'm trying to remove the extensions from my pages in the URL bar.

Example: ../about.php => ../about

The code I'm using right now in my htaccess file is one I found here:

RewriteEngine on

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [QSA,L]

Stored it in the root of my website folder (same folder that contains the 2 webpages).

But this doesn't seem to work.. Live example here. What am I doing wrong?

Community
  • 1
  • 1
jnb13
  • 63
  • 6
  • Not sure what you are trying to achieve. For now it looks like it makes no sense really. – Marcin Orlowski Aug 27 '14 at 11:00
  • I'm trying to make my URL's cleaner by removing the .php from the url so that people can access the site by going to www.example.com/about instead of www.example.com/about.php – jnb13 Aug 27 '14 at 11:31
  • This is actually not how you do friendly urls – Marcin Orlowski Aug 27 '14 at 11:33
  • If you want your URL to look like .../about, then you need to make your link point to .../about. Read the duplicate reference, please. – deceze Aug 27 '14 at 11:39

1 Answers1

0

You can use this code in root .htaccess for removing .php extension:

RewriteEngine on

# redirect /dir/file.php to /dir/file, important to use %{THE_REQUEST}
RewriteCond %{THE_REQUEST} \s/+(?:index)?(.*?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]

# rewrite /dir/file to /dir/file.php
# %{DOCUMENT_ROOT}/$1\.php -f makes sure there is a matching .php file for the
# given name
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]

PS: For SEO purposes you must have first 301 rule in place otherwise it will be bad for SEO since both /about and /about.php will serve same content.

Reference: Apache mod_rewrite Introduction

Apache mod_rewrite Technical Details

Community
  • 1
  • 1
anubhava
  • 664,788
  • 59
  • 469
  • 547
  • Just uploaded this in my htaccess file but doesn't seem to work.. To be clear the htaccess file is in the same folder als my index.php and about.php file – jnb13 Aug 27 '14 at 11:32
  • That means you are not using it in DocumentRoot folder. Try updated code now. – anubhava Aug 27 '14 at 11:35
  • Edit: I've placed it one folder above and now I can access the about page by going to www.jnbwd.be/test/about! But when I click on the link from the index page there is still the exentsion in the URL Edit: gonna try the new code! – jnb13 Aug 27 '14 at 11:35
  • Thanks man! Works great :) – jnb13 Aug 27 '14 at 11:37
  • Use updated code in root .htaccess **(one level above test)** – anubhava Aug 27 '14 at 11:38
  • 2
    Thanks man! Works great :) Is there any way you could explain the code :)? – jnb13 Aug 27 '14 at 11:39
  • Sure adding explanation and reference link to answer. – anubhava Aug 27 '14 at 11:40
  • @jnb13 You shouldn't use 301 redirects to "make links pretty". It incurs a double roundtrip and will break POST submits. Instead, actually make your links point to what they're supposed to look like in your HTML source. – deceze Aug 27 '14 at 11:44
  • While agreeing totally with the point @deceze raised that links in HTML should be updated to to be without `.php` but first 301 rule is still needed for the old search engine links or other incoming links from other sites. – anubhava Aug 27 '14 at 11:48
  • @anubhava Yes, such redirects are useful for SEO if the site is already live. But please make it clear that it's for that purpose, and is not the primary means of removing the extension from the URL. – deceze Aug 27 '14 at 11:52
  • I should have stated maybe that this was for SEO purpose.. But SEO wise this solution is ok? – jnb13 Aug 27 '14 at 11:53
  • Yes for SEO purposes you must have first 301 rule in place otherwise it will be bad for SEO since both `/about` and `/about.php` will serve same content. – anubhava Aug 27 '14 at 11:56