在index.php?page=之后写入的任何内容都不会导致404错误


Anything written after index.php?page= loads website not causing 404 error

我有基于PHP的HTML的动态网站。我使用索引.php作为布局文件。要加载子页面(例如联系人或新闻),我使用 url index.php?page=contact or index.php?page=news。这没有问题。

问题是当用户在index.php?page=(index.php?page=anything)之后写入任何内容时。这应该执行 404 错误,但加载没有内容的网站(仅布局文件)。如何管理?

我使用 .htaccess 制作友好链接,所以我的所有网址看起来都像 http://mywebsite.com/contat 而不是 http://mywebsite.com/index.php?page=contact。

索引的代码片段.php:

<div id="links">
<hr class="line" size="1">
<a class="link1" href="/news">NEWS</a>
<a class="link1" href="/technical-info">TECHNICAL INFO</a>
<a class="link1" href="/entry-system">ENTRY SYSTEM</a>
<a class="link1" href="/starting-list">STARTING LIST</a>
<a class="link1" href="/results">RESULTS</a>
<a class="link1" href="/contact">CONTACT</a>
</div>
<div id="content">
<hr class="line" size="1">
<div style="margin-left: 35px; margin-right: 35px;">
<?php
if(empty($_GET['page']) or $_GET['page']=='news'){
include("news.php");
}
if($_GET['page']=="technical-info"){
include("technical-info.php");
}
if($_GET['page']=="entry-system"){
include("entry-system.php");
}
if($_GET['page']=="starting-list"){
include("starting-list.php");
}
if($_GET['page']=="contact"){
include("contact.php");
}
if($_GET['page']=="results"){
include("results.php");
}
if($_GET['page']=="add-entry"){
include("entrysystem/add-entry.php");
}
?>
</div>
</div>

.ht访问代码:

Options -Indexes
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(news)/?$ index.php?page=$1 [L]
RewriteRule ^(technical-info)/?$ index.php?page=$1 [L]
RewriteRule ^(entry-system)/?$ index.php?page=$1 [L]
RewriteRule ^(starting-list)/?$ index.php?page=$1 [L]
RewriteRule ^(results)/?$ index.php?page=$1 [L]
RewriteRule ^(contact)/?$ index.php?page=$1 [L]
RewriteRule ^(add-entry)/?$ index.php?page=$1 [L]

将 if 块替换为 elseif 块,最后添加一个 else 条件。所以它应该看起来像这样:


If($_GET['page']=='blah'){
       .....
}elseif($_GET['page']=='foo'){
       ....
}elseif($_GET['page']=='another-page'){
       ....
}else{
    header("HTTP/1.0 404 Not Found");
    include("your_custom_404_page.php");
}