2

How am I able to hide a .php extension in an URL address, so that this address:

http://www.thesite.com/somefile.php

would look like:

http://www.thesite.com/somefile

without the use of the .htaccess file. The reason for that being because I have many directories and would want to hide the extension on all those files in every directory. I have tried to set expose_php to off, and this still fails with error 404.

I am using PHP 5.3.10 and Apache server.

4

3 Answers 3

4

Although you specifically said no, using the .htaccess file would remove the .php extension from all PHP files in all subdirectories in a site. I think that is what you are going for.

<IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME}\.php -f
  RewriteRule ^(.*)$ $1.php
</IfModule>

Putting that into the .htaccess file will remove all the .php file extensions. Or you could put it directly into the webserver's configuration files.

Sign up to request clarification or add additional context in comments.

Comments

2

You can achieve this with URL rewriting. If you don't want to use .htaccess, you can write the rule in your host configuration file.

1 Comment

How exactly and where? Where is the host configuration file and what is its name? E.g., can you provide an example?
0

URL rewriting is quite a heavy-handed approach when the much simpler MultiViews exists

In a <Directory>, <Location> or <Files> section of your httpd configuration (or in .htaccess if allowed by AllowOverride)...

Options MultiViews

From the documentation...

The effect of MultiViews is as follows: if the server receives a request for /some/dir/foo, if /some/dir has MultiViews enabled, and /some/dir/foo does not exist, then the server reads the directory looking for files named foo.*, and effectively fakes up a type map which names all those files, assigning them the same media types and content-encodings it would have if the client had asked for one of them by name

Note that conflicting files and directories may cause problems so try not to have both foo.php and foo.html or foo/ in the same directory.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.