【问题标题】:Format url to friendly url using .htaccess使用 .htaccess 将 url 格式化为友好的 url
【发布时间】:2017-12-20 12:17:57
【问题描述】:

我尝试使用 htaccess 将我的网址格式化为友好的网址

这是我的网址

http://localhost/index.php?go=product&id=32

我正在使用请求方法

$id = $_REQUEST['id'];
//Do some thing with php code to get info product from Mysql
$Title = $row['title'];
//Example: Computer 4GB 2CPU

但是当我想对我的网站进行 SEO 时,它并不好,我希望我的网址是这样的:

http://localhost/product/32/Computer-4GB-2CPU

【问题讨论】:

    标签: .htaccess


    【解决方案1】:

    所以在你的 .htaccess 中,添加这个:

    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}\.php -f
    RewriteRule ^(.*)$ $1.php
    
    RewriteRule ^([0-9a-zA-Z]+)/([0-9a-zA-Z]+)/([0-9a-zA-Z]+)$ index.php?go=$1&id=$2&product=$3 [NC,L]
    

    在您的<head> 中添加以下内容:

    <base href="/">
    

    这样图像和 CSS 才能正常工作。

    为产品添加一个get方法来获取名称,它应该可以工作,如果它不继续评论。

    【讨论】:

    • 看不到css,js,img。我正在格式化 RewriteRule ^([a-zA-Z0-9_-]+)-([0-9]+)$ index.php?go=product&amp;id=$2 它可以工作,但看起来很糟糕
    • @CaoHữuThànhHuy 我编辑了上面的答案,请参阅 .htaccess 内容下方的内容。如果我帮助了你,请点击复选标记。
    • 看起来像RewriteRule ^([0-9a-zA-Z]+)/([0-9a-zA-Z]+)/([0-9a-zA-Z]+) index.php?go=product&amp;id=$2 [NC,L] 就很好看。我编辑,它的工作,谢谢你
    • @CaoHữuThànhHuy 太好了!祝您项目顺利!
    猜你喜欢
    • 1970-01-01
    • 2013-05-24
    • 2013-08-31
    • 2012-06-03
    • 1970-01-01
    • 2021-07-23
    • 1970-01-01
    • 2014-12-12
    相关资源
    最近更新 更多