homepage Welcome to WebmasterWorld Guest from 54.226.252.142
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
Can't hide PHP extension in .htaccess file
cosmoyoda




msg:3609510
 9:21 pm on Mar 24, 2008 (gmt 0)

Hello,

I want to hide every .php extension in my site. I tried accomplishing this by writing the following code into my .htaccess file:

RewriteEngine on

AddType 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!

 

hdogan




msg:3609654
 12:48 am on Mar 25, 2008 (gmt 0)

You should use ForceType directive to reach files without extension.

ForceType application/x-httpd-php

PS: You don't need to enable rewrite module.

cosmoyoda




msg:3609655
 12:49 am on Mar 25, 2008 (gmt 0)

Still not working, hdogan.

Maybe there's a configuration problem in my computer? I'm running Mac OS X Leopard and I'm using MAMP.

jdMorgan




msg:3609681
 1:51 am on Mar 25, 2008 (gmt 0)

If you want to request the URL-path /my_file and have the contents served from (generated by) my_file.php, then you need to rewrite the extensionless requests to the requested path plus ".php"

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 Options line may or may not be required. Or it may or may not be allowed, depending on your AllowOverride setting in httpd.conf. Only testing will tell.

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]

cosmoyoda




msg:3615783
 11:46 pm on Mar 31, 2008 (gmt 0)

Hello WebmasterWorlders,

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!

jdMorgan




msg:3616202
 1:46 pm on Apr 1, 2008 (gmt 0)

Make sure your php script checks the requested URL and returns a 404-Not Found if proper and unique content cannot be generated for that URL. Otherwise, *any URL whatsoever* requested from your server will result in a 200-OK response. This makes the URL-space of your domain "infinite" and search engines will detect this and arbitrarily limit the depth to which they will crawl your site. This will limit the depth and ranking of your listings in search results.

The solution I posted above prevents this problem, and is more efficient as well.

Jim

cosmoyoda




msg:3624126
 12:15 am on Apr 11, 2008 (gmt 0)

Hello jdMorgan,
Thanks for pointing that out. It is extremely important and I can't believe I forgot about it. However, I tried your code and it's not working anymore. Even my code isn' working. With your code I get an "Internal Server Error" and no extensionless URLs and with my code I don't get an error, just a 404 Not Found error because it doesn't recognize files without the .php extension.

Am I doing something wrong here?

jdMorgan




msg:3624149
 12:45 am on Apr 11, 2008 (gmt 0)

You're right. There was a typo in the RewriteRule I posted. I corrected it in the post above. Also, the rule I posted was meant for use in .htaccess, and not in httpd.conf or conf.d, and will have to be modified for use in either of those server config files:

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]

If you had your code working and then it stopped working, then the problem has nothing to do with the code posted here. Something else has changed.

Be sure to completely flush your browser cache before testing after you change any server-side code.

Jim

cosmoyoda




msg:3624174
 1:44 am on Apr 11, 2008 (gmt 0)

Hey. Thanks for correcting the typo. However, it seems that it doesn't work, no matter what I do.

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!

jdMorgan




msg:3624176
 1:53 am on Apr 11, 2008 (gmt 0)

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

cosmoyoda




msg:3624177
 1:56 am on Apr 11, 2008 (gmt 0)

Yep, I agree.

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.

jdMorgan




msg:3624178
 1:58 am on Apr 11, 2008 (gmt 0)

In httpd.conf:

mod_rewrite must be installed and loaded.
AllowOverride Options must be enabled.

Jim

cosmoyoda




msg:3624264
 4:31 am on Apr 11, 2008 (gmt 0)

jdMorgan,
Turns out they are already enabled on my computer.

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!

coopster




msg:3624953
 9:45 pm on Apr 11, 2008 (gmt 0)

Apache mod_rewrite enabled in php.ini? That's a new one to me. Ask your host what entry you need to make in your php.ini as they have so directed in order to enable mod_rewrite. I think there is a bit of confusion going on there. I'm dying to hear their answer.

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.

cosmoyoda




msg:3625051
 12:46 am on Apr 12, 2008 (gmt 0)

Yeah, no kidding coopster. I thought it was pretty weird when they told me I could enable mod_rewrite using a php.ini file.

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.

cosmoyoda




msg:3626022
 10:34 pm on Apr 13, 2008 (gmt 0)

jdMorgan and coopster,

I dunno what happened, but I tried my old
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
last night and it worked like a charm. So then I tried jdMorgan's code:
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]
and 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.

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

jdMorgan




msg:3626027
 10:45 pm on Apr 13, 2008 (gmt 0)

Where are you putting the code, in httpd.conf, or in example.com/.htaccess?

Jim

cosmoyoda




msg:3626050
 12:02 am on Apr 14, 2008 (gmt 0)

example.com/.htaccess file, as usual.

jdMorgan




msg:3626086
 12:48 am on Apr 14, 2008 (gmt 0)

The code you say you tried was meant for use in httpd.conf, as stated in the my post above. The code has to be changed for use in .htacess:

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]

Jim

g1smd




msg:3629689
 1:20 am on Apr 18, 2008 (gmt 0)

An additional issue:

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.

cosmoyoda




msg:3635632
 7:08 am on Apr 26, 2008 (gmt 0)

Hi g1smd,

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!

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved