I would like to hide php extension in url and in the responses of my Apache
I tried it with the following tipycal configuration:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [L,QSA]
DirectoryIndex index.php index.html
DocumentRoot /var/www/mysite
<Directory /var/www/mysite>
Options -Indexes -MultiViews FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>
...
but I do not understand why Apache adds a set of headers that I think are unnecessary. for example about this url:
http://mysite/page
Edit: disabling Content Negotiation with -MultiViews cause:
# tailf /var/log/apache2/error.log
[Sun Jan 19 16:35:23 2014] [error] [client 127.0.0.1] Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace.
[Sun Jan 19 16:35:23 2014] [debug] core.c(3063): [client 127.0.0.1] r->uri = /var/www/index.php
[Sun Jan 19 16:35:23 2014] [debug] core.c(3069): [client 127.0.0.1] redirected from r->uri = /var/www/index.php
...
[Sun Jan 19 16:35:23 2014] [debug] core.c(3069): [client 127.0.0.1] redirected from r->uri = /var/www/index.php
[Sun Jan 19 16:35:23 2014] [debug] core.c(3069): [client 127.0.0.1] redirected from r->uri = /page
[Sun Jan 19 16:35:23 2014] [debug] mod_deflate.c(615): [client 127.0.0.1] Zlib: Compressed 533 to 322 : URL /var/www/index.php
# tailf /var/log/apache2/rewrite.log
127.0.0.1 - - [19/Jan/2014:18:42:06 +0100] [mysite/sid#7fa89ea62d58][rid#7fa89ed69340/initial] (1) pass through /page
127.0.0.1 - - [19/Jan/2014:18:42:06 +0100] [mysite/sid#7fa89ea62d58][rid#7fa89ed69340/initial] (1) [perdir /var/www/] internal redirect with /var/www/index.php [INTERNAL REDIRECT]
127.0.0.1 - - [19/Jan/2014:18:42:06 +0100] [mysite/sid#7fa89ea62d58][rid#7fa89ed75738/initial/redir#1] (1) pass through /var/www/index.php
127.0.0.1 - - [19/Jan/2014:18:42:06 +0100] [mysite/sid#7fa89ea62d58][rid#7fa89ed75738/initial/redir#1] (1) [perdir /var/www/] internal redirect with /var/www/index.php [INTERNAL REDIRECT]
127.0.0.1 - - [19/Jan/2014:18:42:06 +0100] [mysite/sid#7fa89ea62d58][rid#7fa89ed77ad0/initial/redir#2] (1) pass through /var/www/index.php
...
...
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [L,QSA]
DocumentRoot /var/www/mysite
<Directory /var/www/mysite>
Options -Indexes -MultiViews FollowSymLinks
...
Try this code (But both php and non-php tagged will work with it, unless you make it redirect them)
RewriteEngine On
RewruteRule ^(.*)$ /$1.php
For more information check this link - http://www.askapache.com/htaccess/modrewrite-tips-tricks.html
You think that this is mod_rewrite
that takes care of the renaming, but it is actually mod_negotiation, more specifically the Multiviews
Option, because apparently, mod_negotiation
is executed earlier than mod_rewrite
in the request lifecycle in Apache 2.0 and later.
What's happening is that when requesting http://mysite/page
and a page
file does not exists in the corresponding directory, mod_negotiation will try to locate files with the pattern page.*
.
If a file named page.php
is indeed present, mod_negotiation will then tell Apache to handle the current request as if http://mysite/page.php
had been requested, and sets a few response headers, that are used mainly to help client-side caching:
Vary: negotiate
Content negotiation is being used for this request.
TCN: choice
This means the resource is transparently negotiated, choice means the 'best' variant has been returned, and the original name for the variant is present in the Content-Location
header.
Content-Location: page.php
This is set in order for the browser to know that page.php
is the best variant for page
.
So the solution in your case is to disable content negotiation, either by ensuring thatOptions -Multiviews
is present, or by making sure mod_negotiation
is not loaded at all, so you can be certain that mod_rewrite
will handle the renaming instead, without adding those headers.