将我的 URL 重写为 "Pretty URLs" 会导致任何 404 错误吗?



美好的一天!我有一个有关从URL中删除文件扩展的问题。

如果我要使用以下规则从我的网站URL中删除.php扩展程序,那么任何先前链接到任何网站页面上的任何链接都会中断吗?

例如www.site.com/archives.php变成www.site.com/archives导致www.site.com/archives.php的任何链接是否仍会导致www.site.com/archives,还是会导致404?

重写规则:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php

预先感谢。

我建议您一个更好的解决方案。

RewriteEngine on
# dont rewrite for directories
RewriteCond %{REQUEST_FILENAME} !-d
# rewrite everything else to index.php
RewriteRule .* index.php [L]

现在您可以在index.php中使用变量

$_SERVER['REQUEST_URI']

您可以读取要求的文件

示例:

url:http://my-site.de/archives

然后,在请求var中,您将拥有字符串"存档",现在您可以用strtolower解析字符串以较低(我更喜欢此。

另一个示例:

url:http://my-site.de/archives/latest/show

请求变量包含字符串" Archives/最新/show"

现在,您可以使用目录在同一目录中找到服务器上的文件,也可以使用路径作为请求的参数。

php中带有PHP结束的解决方案在PHP中看起来像这样:

<?php
$requestedPath = $_SERVER['REQUEST_URI'];
if (substr(strtolower($requestedPath),-4) != '.php') {
    $requestedPath .= '.php';
}
if (file_exists($requestedPath)) {
    include_once($requestedPath);
}
?>

相关内容

最新更新