I'm currently been trying to figure out how to remove .php
extension from URL bar. However when I remove .php
it gives me a blank white page.
Could someone please be able to explain why this would be happening?
I've added to the php.ini
: expose_php=Off
and also got this in my .htaccess
:
# Return 404 if original request is /foo/bar.php
RewriteCond %{THE_REQUEST} "^[^ ]* .*?\.php[? ].*$"
RewriteRule .* - [L,R=404]
Then started to write the following for URL
:
<?php
function parseCurrentURL() {
$pageURL = 'http';
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return $pageURL;
}
if(parse_url(parseCurrentURL(), PHP_URL_PATH)){
$url = parse_url(parseCurrentURL(), PHP_URL_PATH);
switch(substr($url, strrpos($url, '/') + 1)){
case 'forums':
break;
}
}
?>
Example with .php
:
Example without .php
:
There is my htaccess code check if this work for you
# Default directory
DirectoryIndex index.php
# ERROR FILES
RewriteEngine On
ErrorDocument 404 /page404.php
ErrorDocument 500 /page500.php
# Remove the need for www in Your URL
RewriteCond %{HTTP_HOST} ^xxxx.pt$ [NC]
RewriteRule (.*) http://www.xxxx.pt/$1 [R=301,L]
# Exclude administrador folder
RewriteRule ^(administrador)($|/) - [L]
# Rewrite URLs
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f [NC]
RewriteRule ^ %{REQUEST_URI}.php [L]
##Expire headers
<IfModule mod_expires.c>
# Enable expirations
ExpiresActive On
# Default directive
ExpiresDefault "access plus 1 month"
# My favicon
ExpiresByType image/x-icon "access plus 1 year"
# Images
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType image/jpg "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
# CSS
ExpiresByType text/css "access plus 1 month"
# Javascript
ExpiresByType text/javascript "access plus 1 month"
</IfModule>
The problem can be resolved with apache .htaccess configuration
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
more information here https://alexcican.com/post/how-to-remove-php-html-htm-extensions-with-htaccess/
I current use this code in my .htaccess to remove the extension .php and I never get a blank page
# Rewrite URLs
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]