簡體   English   中英

mod_rewrite不會更改URL

[英]mod_rewrite not changing URLs

因此,我剛剛開始使用Apache的mod_rewrite模塊,但遇到了一個似乎無法解決的問題。 我想要的是當用戶手動鍵入URL或鏈接到頁面時,使地址欄顯示干凈的URL。 現在,輸入URL時我會得到干凈的URL,但是當鏈接到頁面時,查詢字符串仍顯示在地址欄中。 例如:

輸入時,myDomain.com / first將帶我進入myDomain.com/index.php?url=first的頁面,並在地址欄中顯示myDomain.com/first。

但是,當單擊類似href =“ index.php?url = first”的鏈接時。 當我希望它顯示myDomain.com/first時,地址欄顯示myDomain.com/index.php?url=first。

這是我的.htaccess文件,它與我的索引文件位於同一文件夾中:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9_\-]+)/?$ index.php?url=$1 [NC,L]
</IfModule>

這是我的索引文件:

<?php
define('ROOT_DIR', dirname(__FILE__) . '/'); // Define the root directory for use in includes 
require_once (ROOT_DIR . 'library/bootstrap.php');

$url = strtolower($_GET['url']);

include(ROOT_DIR . 'views/headerView.php');

switch($url)
{
    case "first": include(ROOT_DIR . 'views/firstPageView.php');
        break;
    case "second": include(ROOT_DIR . 'views/secondPageView.php');
        break;
    default: include(ROOT_DIR . 'views/homeView.php');
} 

include 'views/footerView.php';
?>

這是homeView.php:

<p>This is the home page.</p>
<p>To the first page. <a href="index.php?url=first">First Page</a></p>
<p>To the second page. <a href="index.php?url=second">Second Page</a></p>

在我的鏈接問題上的任何建議或幫助,將不勝感激,在此先感謝您。

但是,當單擊類似href =“ index.php?url = first”的鏈接時。 當我希望它顯示myDomain.com/first時,地址欄顯示myDomain.com/index.php?url=first。

您必須鏈接到“干凈” URL。 請記住,您不是在這里重定向。 您正在重寫! 這意味着您必須更改此設置:

<p>This is the home page.</p>
<p>To the first page. <a href="index.php?url=first">First Page</a></p>
<p>To the second page. <a href="index.php?url=second">Second Page</a></p>

對於這樣的事情:

<p>This is the home page.</p>
<p>To the first page. <a href="/url/first">First Page</a></p>
<p>To the second page. <a href="/url/second">Second Page</a></p>

看這兩行:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

這意味着: 如果URL指向真實文件或文件夾,請不要嘗試遵循以下規則。

當您使用myDomain.com/index.php?url=first ,它指向一個實際文件: index.php 然后,您的規則將不會被嘗試。

必須始終在代碼中使用像myDomain.com/first這樣的干凈URL。

暫無
暫無

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

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