我想通过 htaccess 更改我的地址栏。
我的链接是:http://englishforyou.ir/index.php?content=about
我想将其更改为 http://englishforyou.ir/content/about
。
我的 htaccess 代码是:
RewriteEngine on <br>
RewriteRule ^content/([A-Za-z0-9-]+)/?$ index.php?content=$1 [NC]
但是这不起作用。您可以在这里测试:englishforyou.ir
我的索引文件是:
<?php
require_once 'includes/function.php';
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>
<body>
<div>
test
<a href="index.php?content=about" class="drop">About me</a>
</div>
<?php loadContent('content', 'mainpage'); ?>
</body>
</html>
我的函数文件是:
function loadContent($where, $default='') {
// Get the content from the url
// Sanitize it for security reasons
$content = filter_input(INPUT_GET, $where, FILTER_SANITIZE_STRING);
$default = filter_var($default, FILTER_SANITIZE_STRING);
// If there wasn't anything on the url, then use the default
$content = (empty($content)) ? $default : $content;
// If you found have content, then get it and pass it back
if ($content) {
// sanitize the data to prevent hacking.
$html = include $content.'.php';
return $html;
}
}
我的关于我的文件是:
<div>
<h4>
This page is about me.
<a href="index.php?content=mainpage" class="drop">home</a>
</h4>
</div>
最佳答案
您的 .htaccess 文件正确并且正在运行。
您可能需要在没有缓存的情况下刷新浏览器,或者等待它自行刷新。
如果您使用的是 Firefox 或 Chrome,请使用 Ctrl+F5
刷新覆盖缓存。
注意:您只需使用网址 http://englishforyou.ir/content/about
而不是 http://englishforyou.ir/index.php?content=about
编辑
将此行添加到您的 .htaccess 以从 http://englishforyou.ir/index.php?content=about
重定向到 http://englishforyou.ir/content/about
RewriteRule ^index\.php\?content\=(.*)$ /content/$1 [R=301,L]
关于php - 通过 htaccess 更改地址栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31787589/