Forum Moderators: phranque
I want to hide every .php extension in my site. I tried accomplishing this by writing the following code into my .htaccess file:
RewriteEngine onAddType application/x-httpd-php .htm .html
I tried browsing [localhost...] It worked.
But visiting [localhost...] gave me a 404 Error File Not Found.
Any ideas why this rule is not working?
Thank you!
I show here a method that checks to see if the file exists first, which will allow you to do the same thing for .htm and .html files by copying this rule if you wish:
Options +FollowSymLinks
RewriteEngine on
#
# If requested URL-path plus ".php" exists as a file
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
# Rewrite to append ".php" to extensionless URL-path
RewriteRule ^(([^/]+/)*[^.]+)$ /$1.php [L]
The pattern supports rewriting requests at any "directory level" as long as the final path-part does not contain a period (because that would indicate a file extension).
Flush your browser cache before testing any new server-side code, including this code.
For more information, see the references cited in our forum charter, and the tutorials in our forum library (links at top of this page).
Jim
[edit] Typo corrected as noted in post below. [/edit]
[edited by: jdMorgan at 12:39 am (utc) on April 11, 2008]
Thanks a lot for your answers and I'm glad this is finally working now. I'll write here what I did to solve the problem just in case if someone else is having the same problem, they can read this and fix it in seconds. The solution was pretty simple, here's what I did:
1. Completely stop your Apache servers.
2. Open up your browser and clean the cache completely.
3. Open .htaccess and add the following code and save file after:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
4. Start your Apache server.
5. Open up your browser: Now you can browse any PHP file without having to put .php at the end of the URL!
Once again, thanks a lot! Extensionless URLs rules!
The solution I posted above prevents this problem, and is more efficient as well.
Jim
Am I doing something wrong here?
Options +FollowSymLinks
RewriteEngine on
#
# If requested URL-path plus ".php" exists as a file
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
# Rewrite to append ".php" to extensionless URL-path
RewriteRule [b]^/([/b]([^/]+/)*[^.]+)$ /$1.php [L]
Be sure to completely flush your browser cache before testing after you change any server-side code.
Jim
I tried that on my local machine, using my PHP Apache web server and same problem occurs. Just to make sure it wasn't only my machine who had a problem with the .htaccess file Rewrite Code, I tried the same code in a .htaccess file in my server with the hosting company. Same problem. No extensionless URLs.
I also cleared all the cache of Firefox and tried restarting my Apache server on my local machine. Nothing worked. =(
That coded worked for a day, but then the next day I turned on my computer and it wasn't working.
Any ideas? Thanks!
That code worked for a day, but then the next day I turned on my computer and it wasn't working.Any ideas? Thanks!
No, sorry. I have no idea. If you didn't change anything else, and it just stopped working, then there is no explanation.
Let me be very clear about the code, though. It must be changed if you move it from httpd.conf to .htaccess, or from .htaccess to httpd.conf. The RewriteRule pattern *must* have a leading slash in httpd.conf, and it *must not* have a leading slash in .htaccess.
mod_rewrite code is very critical: If you have one single character wrong, it will not work.
Jim
However, do you think maybe there's something I should look for in my httpd.conf file? A small change I need to do in order to make mod_rewrite work on my machine? I haven't changed the file, btw, but if there's any configuration I need to do, please let me know and it might solve the problem.
However, I just found out by checking my PHP server with my hosting company using the phpinfo() function that mod_rewrite IS NOT enabled.
They said I can enable it using a php.ini file. I already have that file there, all I need is a few lines of code to enable mod_rewrite. Do you know what I need to write there in order to enable it?
Thanks again!
Also, you mentioned it was working and then stopped. Aside from the above issue, if you are using the code from jdMorgan in an .htaccess file it must be in the DOCUMENT_ROOT directory of your site. If you tried to use it in a subdirectory of the document root it is going to fail.
And yes, my .htaccess is always in the root folder. It's always in my "/" folder, which would be for instance "http://www.mysitehere.com/". I never ever place .htaccess files in sub directories, unless I only need it for that particular directory. I'll try contacting my hosting company and fixing this problem and I'll tell you if I find a solution. Thanks for all the help once again.
I dunno what happened, but I tried my old
RewriteEngine onlast night and it worked like a charm. So then I tried jdMorgan's code:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
Options +FollowSymLinksand it still didn't work. I would love to use his code instead, since it fixes a few bugs with SEO and it's more complete.
RewriteEngine on
#
# If requested URL-path plus ".php" exists as a file
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
# Rewrite to append ".php" to extensionless URL-path
RewriteRule ^/(([^/]+/)*[^.]+)$ /$1.php [L]
I also tried this code with my hosting company and it worked perfectly. Not jdMorgan's though, unfortunately. And about the php.ini thingy, forget it. I talked to the customer service of my hosting company and the guy told me mod_rewrite is already enabled and there is no such thing as enabling it through php.ini. The guy I talked to before this one told me I COULD enable mod_rewrite using php.ini, but he was wrong. Mod_rewrite is already enabled, even thought I can't see it using the phpinfo() function in my hosting company server. Turns out they don't show that to you for security reasons. =/
I still dunno why jdMorgan's code doesn't work though. It's kinda strange. Does it work for you? Maybe it's a configuration in my php.ini or httpd.conf file, I dunno. =S
RewriteEngine on
#
# If requested URL-path plus ".php" exists as a file
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
# Rewrite to append ".php" to extensionless URL-path
RewriteRule ^(([^/]+/)*[^.]+)$ /$1.php [L]
With extensionless URLs, every page of your site now has two URLs that it can respond to and return "200 OK". That's a duplicate content issue waiting to catch you out.
If you're not fixing up www and non-www that's four URLs for every page of content, and if you're not fixing up index file filenames, too, then that's six different URLs for every index page found.
Thanks for reminding me that. It's extremely important! Although, I don't really understand how you can solve this problem. Also, can you give me a better example with more details or give me a good site where I could find information about this (if not a site, at least the keyworks I should look up on Google), because I'm kinda lost.
Thank you!