php - 通过 htaccess 更改地址栏

标签 php .htaccess

我想通过 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/

相关文章:

php - htaccess 重写 url,如 Stack Overflow

apache - 为什么.htaccess没有任何效果?

php - SHA 函数问题

php - Doctrine 2 : Warning: Illegal offset type in isset or empty

css - 子域无法加载 css 或图像

regex - 将 "http://x.com/address.1xxxx"转换为 "http://address.y.com"的 htaccess 和正则表达式是什么

php - 如何检查服务器上是否启用了 mod_rewrite?

php - 如何将 WordPress 类别选择限制为只有一个?

php - 通过加密安全地从 PHP 连接到 MSSQL?

php - Wordpress 列出类别,上面至少有 5 个帖子