[英]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.