使用.htaccess在URL中重写查询字符串

时间:2017-01-17 19:33:32

标签: .htaccess url url-rewriting

如何将网站的网址从“website.com/example.php?site=youtube”重写为“website.com/example/youtube”,其中网站是变量? 我应该更改php文件中的url吗?

2 个答案:

答案 0 :(得分:0)

您可以在.htaccess文件中使用此功能:

RewriteEngine On
RewriteRule ^example/([^/]*)$ /example.php?site=$1 [L]

这将为您留下以下网址:http://website.com/example/youtube。在测试之前,请确保清除缓存。

答案 1 :(得分:0)

 $connection = mysqli_connect("localhost" , "root" , "" , "test");

 $query = "SELECT * FROM websites";

 $select_all_sites = mysqli_query($connection,$query);

 while ($row = mysqli_fetch_assoc($select_all_sites)) {
          $site_title = $row['site_title'];

echo "<a class='item' href='site.php?website=$site_title'> $site_title</a>";

在另一个名为site.php的页面上,我使用此代码从sites.php页面获取变量

  function myname(){
  if(isset($_GET['website'])){
  $variable = $_GET['website'];
  echo $variable;
    }
  }