0

I was up to remove the file name or extensions from my webpage address. There were some solutions for doing this (.htaccess) but my .htaccess file was empty. I wanna know that how can i do this? Turning this:

http://www.example.com/page.php

Into This:

http://www.example.com/page

Thanks for reading.

Hossein MK
  • 23
  • 5

2 Answers2

0

Assuming that the page.php does exist in your root folder, as a physical file, then you can use:

RewriteEngine On
RewriteRule ^([^\.]+)$ $1.php [NC,L]

If page.php does not exists, but it is a product of a rewrite rule, you can use something like:

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

The two extra rules say something: if file in browser does not exist and it is not an existing folder, then proceed with the rewrite.

You will need to change your links in your site, for the above rules to work. Something like:

<a href="http://www.example.com/page">Some fancy page</a>
andrew
  • 2,058
  • 2
  • 25
  • 33
  • Your solution will produce an internal server error for any url `example.com/nonexisting` where `nonexisting.php` does not exist. It will keep trying to rewrite until it hits the internal recursion limit. – Sumurai8 Aug 05 '14 at 12:50
0

Make sure that mod_rewrite is enabled in your main config file and that the FollowSymLinks option is enabled, and that you restart Apache to let those changes take effect. Then add the following to a .htaccess file in your www-root:

RewriteCond %{REQUEST_URI} !\.php$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ $1.php [L]

The first condition will make sure your url does not yet end with php. If it does, it is pointless to add php to it, now is it? The second condition checks if the file that is requested does not exist. If it would exist, we want to show the existing file: for example an image, or css or js file. The last line internally rewrites to the file with .php behind it.

Sumurai8
  • 20,333
  • 11
  • 66
  • 100