0

i have a .htaccess file on my server and i am using mod_rewrite to hide the .php extension in the url. so http://www.mysite.com/home.php becomes http://www.mysite.com/home

however i get a 404 error message saying the page was not found. am i missing something, do i need to change my links to an absolute path? because even if a user tries to type in http://www.mysite.com/home or http://www.mysite.com/home.php it still says the page was not found.

here's whats in my htacces file:

RewriteEngine On

RewriteCond %{THE_REQUEST} ^\w+\ /(.*)\.php(\?.*)?\ HTTP/
RewriteRule ^ http://%{HTTP_HOST}/%1 [R=301]

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule .* $0.php
2
  • Basically you want to remove your .php extension from your file names?
    – Daryl Gill
    Apr 4, 2013 at 0:30
  • i want the users browser url to not show the .php at the end of the file names, so i have contact_us.php on my server, if a user clicks the link contact us their url will show mysite.com/contact_us (so no .php extension is shown) but at the moment my links can not be found
    – James Pale
    Apr 4, 2013 at 0:32

1 Answer 1

0
RewriteEngine On
# turn on the mod_rewrite engine

RewriteCond %{REQUEST_FILENAME}.php -f
# IF the request filename with .php extension is a file which exists
RewriteCond %{REQUEST_URI} !/$
# AND the request is not for a directory
RewriteRule (.*) $1\.php [L]
# redirect to the php script with the requested filename

Try the example I have posted. This should work to remove your PHP Extension

4
  • can i leave my links alone i.e. they look like this at the moment: <a href="home.php"> or do i need to change them to <a href="home">
    – James Pale
    Apr 4, 2013 at 0:34
  • i tried this but the .php at the end of the file is still shown in the url
    – James Pale
    Apr 4, 2013 at 0:35
  • You can leave this, but with the anchor references the user will be displayed with the .php extension due to you physically referring the user to this page; No errors will be provided if directing to .php or home
    – Daryl Gill
    Apr 4, 2013 at 0:35
  • @JamesPale you are still being shown the .php because using .htaccess does not make automatic changes directly to the URL; if you navigate to mywebsite.com/index you will be presented with your index page, if you wish to manipulate the clients URL, you will need to perform some server sided scripting to re-direct silently
    – Daryl Gill
    Apr 4, 2013 at 0:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.