簡體   English   中英

index.php之后寫的任何東西?page =加載網站不會導致404錯誤

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

我有基於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]

使用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");
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM