1

How to remove .php extension using .htaccess file. I have tried many examples without success. An Internal server error is regularly showing. I am using Godaddy Linux Hosting.

This code is the latest which I am using.

<IfModule mod_rewrite.c> 
     Options All -Indexes 
     Options +FollowSymLinks -MultiViews 
     RewriteEngine On 
     RewriteBase /

     # Unless directory, remove trailing slash 
     RewriteCond %{REQUEST_FILENAME} !-d 
     RewriteRule ^([^/]+)/$ http://a.sprybirds.com/$1 [R=301,L] 

     # Redirect external .php requests to extensionless url 
     RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/ 
     RewriteRule ^(.+)\.php$ http://a.sprybirds.com/$1 [R=301,L] 

     # Resolve .php file for extensionless php urls 
     RewriteRule ^([^/.]+)$ $1.php [L]
</IfModule>

Site is parked on a sub-domain http://subdomain.example.com and folder sequence is just like web-root/sub_folder/site_folder/.

MrWhite
  • 11,643
  • 4
  • 25
  • 40
  • What does your error log say? If it's throwing a 500 Internal, there's some kind of cause and the logfile should give you more detail about why that is. – ldennison Jun 28 '15 at 07:28
  • Can `#` occur in a request at all? – Hagen von Eitzen Jun 28 '15 at 13:48
  • if you are using godaddy, linux shared hosting, than try this article, it will get you the results.http://www.kvcodes.com/2016/02/godaddy-remove-php-extension/ – Kvvaradha Feb 17 '16 at 10:30

2 Answers2

1

The question is missing example URLs, which is really essential for diagnosing such problems, however...

# Resolve .php file for extensionless php urls 
RewriteRule ^([^/.]+)$ $1.php [L]

This only matches URLs with single path segments, ie. URLs that are effectively in the document root, eg. /foo. It won't match URLs with multiple path segments, ie. URLs that are effectively in subdirectories eg. /foo/bar - which would explain why you are getting 404s for "php files inside the subfolder".

You need to remove the slash in the RewriteRule pattern, in order to match any URL depth:

RewriteRule ^([^.]+)$ $1.php [L]

By including a slash in the negated character class you are only matching URLs that don't contain a slash. ie. URLs in the document root only.

(There is no need to backslash escape a literal dot when used inside a character class.)

MrWhite
  • 11,643
  • 4
  • 25
  • 40
0

Do you have a .htaccess file in ./public_html/? If so, what's inside it? The way .htaccess works is by giving the directory closest to the actual root top priority.

As an example:

/home/username/public_html/.htaccess

is given a higher priority (i.e. more control) than

/home/username/public_html/sub_directory/.htaccess

If you're primary .htaccess file is setting rules one way and the sub-directory is setting them another, it's possible you'll run in to issues (i.e. an error).

If you're set in your actual web root, the following lines could be causing some issues as cPanel allows server administrators to set these options by default from within WHM. If these are already set (specifically -Indexes), you'll likely see an error.

Options All -Indexes
Options +FollowSymLinks -MultiViews

Note: The -Indexes directive is normally set by default before clients are even placed on a server (or it should be, unless there's a specific reason not to), FYI. This is more of a security recommendation to prevent directory browsing as not every client is going to know that unless there's an index.[ext] file in every directory, their directories are wide-open for browsing by whoever wants to take a look.


If removing those resolves the error, then you've found the problem :). If removing these lines does not work, trial & error is where we need to start.

Back to the top, look in directories that reside above the directory you're having issues with. Check the .htaccess files and make sure you have "display or show hidden files" checked or set to "yes" in the S/FTP program that you're using as they're not always visible by default.

From here, the best course of action would be to backup each .htaccess file and re-add them one-by-one until you run in to another error. This way you can mark down which file upload brought on the error and we can take a look at that one.


Update #01

Since the culprit for the error most likely was indeed the -Indexes, you can add the following back to the .htaccess file:

Options +FollowSymLinks -MultiViews

If that does not resolve the 404, try this:

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /

# Adds Trailing Slash to the URI as long as it's not for a
# file.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/|#(.*))$
RewriteRule ^(.*)$ http://subdomain.example.com/$1/ [R=301,L]

# Checks to make sure the request is a directory.
# Checks to see if the request matches a file with a .php ext.
# If all is well, push the request through to the matching file.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^\.]+)/$ http://subdomain.example.com/$1.php
MrWhite
  • 11,643
  • 4
  • 25
  • 40
Jonathan
  • 11
  • 2
  • I have a multi domain hosting plan. my primary website is hosted in web-root folder and a htaccess file is also placed in this we-root. And my site http://a.sprybirds.com is hosted in this web-root folder in sequence of folders web-root/sub_folder/site_folder . And I have another htaccess file in this site folder site_folder. I have provided the code in my is of htaccess file inside site_folder. – Kunwar Asif Ali Khan Jun 28 '15 at 11:28
  • @KunwarAsifAliKhan did you try removing the two lines as noted above? If so, did the error persist? Ideally, those two lines should be removed from all .htaccess files while troubleshooting. You can test whether or not you actually need -Indexes. Simply create an empty directory anywhere and access it. If you can browse it, then GoDaddy doesn't have this option enabled (although they really should...). If you can't browse, then this option isn't needed. – Jonathan Jun 28 '15 at 11:34
  • i have removed the above two line from htaccess file and my site is not showing internal sever error but another pages like http://a.sprybirds.com/business-search?find=food-beverage showing 404 error. I have used this htaccess to remove php extension and I have removed .php from all urls of my website. – Kunwar Asif Ali Khan Jun 28 '15 at 11:47
  • @KunwarAsifAliKhan Please see the modified post above. I've added an example I've used which worked on the last Apache 2.x server I was working on. – Jonathan Jun 28 '15 at 12:51
  • I have just paste the code that you have provided in my htaccess file. But I am still facing the problem 404 error when I not use .php but site with .php extension is working check url http://a.sprybirds.com/business-search.php?find=food-beverage – Kunwar Asif Ali Khan Jun 28 '15 at 13:21
  • @KunwarAsifAliKhan Looking over GoDaddy's setup and after reading a few posts about them, it seems you need -MultiViews as well, so we should add that to the above and retest. I've updated my last example to reflect this requirement. – Jonathan Jun 28 '15 at 16:18
  • My friend @Jonathan as you said to remove line Options All -Indexes. I removed this line. Now my site is working well but the php files inside the subfolder which is situated in main site folder in sequence of web-root/main-folder/sub-folder/some-file.php giving 404 error. I am still using my htaccess which I have provided above in the question but as you suggest I have removed the line Options All -Indexes. – Kunwar Asif Ali Khan Jun 30 '15 at 10:13