我有基于HTML和PHP的动态网站。我使用index.php作为布局文件。 要加载子页面(例如联系人或新闻)我使用url index.php?page = contact或index.php?page = news。这没有问题。
问题是当用户在index.php之后写任何内容?page =(index.php?page = anything)。这应该执行404错误但加载没有内容的网站(只有布局文件)。如何管理?
我使用.htaccess建立友好链接,因此我的所有网址都显示为http://mywebsite.com/contat而不是http://mywebsite.com/index.php?page=contact。
index.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>
.htaccess代码:
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]
答案 0 :(得分:0)
使用elseif块替换if块,最后添加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");
}