【问题标题】:How to convert an SEO friendly URL to a PHP page如何将 SEO 友好的 URL 转换为 PHP 页面
【发布时间】:2014-10-26 21:42:35
【问题描述】:

我想显示我的页面网址:

www.mypage.com/?q=picture

代替:

www.mypage.com/picture.html

我不知道如何重定向我想查看的页面。但网址不应显示页面扩展名 我已经尝试过这样的事情,但它不起作用:

<a href="?q=home" style="text-decoration: none; color: #1E90FF; font-size:25px;">Home</a>
<?php
if(isset($_GET['q']) && $_GET['q']!="" )
    {
        $ext=".php";    
        $page=$_GET['q'].$ext;
        if(file_exists($page))
        {
            header('location:$page');
        }
        else 
        {
            header('location:errorpage.php');
        }
    }    
 ?>

【问题讨论】:

  • 你的代码与.htaccess无关,我想你在找mod_rewritehttpd.apache.org/docs/current/mod/mod_rewrite.html
  • 我不是在寻找任何 mod_rewrite.. 我只是想使用我的代码,但是当我尝试我的代码时,它并没有重定向我想要的页面。就是这样
  • 尝试将 $ext 更改为 ".html"

标签: php .htaccess url


【解决方案1】:

使用 .htaccess。创建一个名为 .htaccess 的文件并添加以下代码。希望这有效

#rewrite file
RewriteEngine on

RewriteRule ^?q=([0-9a-zA-Z]+) $1.html

唯一的问题是我可以访问 www.mypage.com/?q=mario,它会查找 mario.html。

如果这不起作用,请使用它。

#rewrite file
RewriteEngine on

RewriteRule ^home home.html
RewriteRule ^picture picture.html

这将使 www.mysite.com/picture 显示所有内容,就像查看图片.html 一样

【讨论】:

  • 我不想使用 .htaccess。无论如何谢谢,寻找更多答案
猜你喜欢
  • 2016-04-29
  • 1970-01-01
  • 2012-08-16
  • 1970-01-01
  • 2016-04-13
  • 2012-09-25
  • 1970-01-01
  • 1970-01-01
  • 2011-05-09
相关资源
最近更新 更多