我目前一直试图找出如何从网址栏中删除.php
扩展名。但是当我删除.php
时,它会给我一个空白页面。
有人可以解释为什么会发生这种情况吗?
我已添加到php.ini
:expose_php=Off
并在.htaccess
:
# Return 404 if original request is /foo/bar.php
RewriteCond %{THE_REQUEST} "^[^ ]* .*?\.php[? ].*$"
RewriteRule .* - [L,R=404]
然后开始为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;
}
}
?>
.php
的示例:
没有.php
的示例:
答案 0 :(得分:1)
我的htaccess代码检查是否适用于您
# 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>
答案 1 :(得分:0)
使用apache .htaccess配置
可以解决问题RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
此处提供更多信息https://alexcican.com/post/how-to-remove-php-html-htm-extensions-with-htaccess/
答案 2 :(得分:0)
我目前在.htaccess中使用此代码删除扩展名.php,我从未得到空白页
# 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]