通过htaccess更改地址栏

时间:2015-08-03 12:42:31

标签: 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>

2 个答案:

答案 0 :(得分:2)

你的.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]

答案 1 :(得分:0)

尝试这样的事情

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.* - [L]
RewriteRule ^system/([^/]*)\.html$ system.php?route=$1

这会将网址http://www.domain.com/system/home.html转换为http://www.domain.com/system.php?route=home 并确保您可以使用.htaccess