简体   繁体   English

mod_rewrite不会更改URL

[英]mod_rewrite not changing URLs

So I just started working with Apache's mod_rewrite module and I've run into a problem I can't seem to figure out. 因此,我刚刚开始使用Apache的mod_rewrite模块,但遇到了一个似乎无法解决的问题。 What I want is to have the address bar display clean URLs either when a user manually types in the URL or when the page is linked to. 我想要的是当用户手动键入URL或链接到页面时,使地址栏显示干净的URL。 Right now I get clean URLs when they're typed in but the query string still shows up in the address bar when the page is linked to. 现在,输入URL时我会得到干净的URL,但是当链接到页面时,查询字符串仍显示在地址栏中。 For example: 例如:

Typing in, myDomain.com/first takes me the page at myDomain.com/index.php?url=first and displays myDomain.com/first in the address bar. 输入时,myDomain.com / first将带我进入myDomain.com/index.php?url=first的页面,并在地址栏中显示myDomain.com/first。

But, when clicking a link like href="index.php?url=first". 但是,当单击类似href =“ index.php?url = first”的链接时。 The address bar displays myDomain.com/index.php?url=first when I want it to display myDomain.com/first. 当我希望它显示myDomain.com/first时,地址栏显示myDomain.com/index.php?url=first。

Here is my .htaccess file located in the same folder as my index file: 这是我的.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>

Here is my index file: 这是我的索引文件:

<?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';
?>

And here is homeView.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>

Any advice or help on my linking problem would be greatly appreciated, thank you in advance. 在我的链接问题上的任何建议或帮助,将不胜感激,在此先感谢您。

But, when clicking a link like href="index.php?url=first". 但是,当单击类似href =“ index.php?url = first”的链接时。 The address bar displays myDomain.com/index.php?url=first when I want it to display myDomain.com/first. 当我希望它显示myDomain.com/first时,地址栏显示myDomain.com/index.php?url=first。

You'll have to link to the "clean" URL. 您必须链接到“干净” URL。 Remember, you're not redirecting here. 请记住,您不是在这里重定向。 You're rewriting! 您正在重写! That mean's you'll have to change this: 这意味着您必须更改此设置:

<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>

To something like this: 对于这样的事情:

<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>

Look those two lines : 看这两行:

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

It mean : if url point on a real file or folder, don't try following rules. 这意味着: 如果URL指向真实文件或文件夹,请不要尝试遵循以下规则。

When you use myDomain.com/index.php?url=first , it point on a real file : index.php . 当您使用myDomain.com/index.php?url=first ,它指向一个实际文件: index.php Then, your rules won't be tried. 然后,您的规则将不会被尝试。

You must always use clean url like myDomain.com/first in your code. 必须始终在代码中使用像myDomain.com/first这样的干净URL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM