【问题标题】:htaccess is not able to redirect article id to article slug generated with php slug functionhtaccess 无法将文章 ID 重定向到使用 php slug 函数生成的文章 slug
【发布时间】:2019-07-10 20:27:07
【问题描述】:

我遵循了许多问题,这些问题发布在 stackoverflow 中,但对我没有帮助,这就是为什么我要完全按照我的要求进行尝试。谢谢采纳..

我尝试了很长时间,但我的问题到现在还没有解决。问题是我试图让我的 url seo 友好,比如 example.com/articles.php?id=''example.com/articles/my-first-article,而 slug 是用 php 生成并插入到数据库中,如下所示。

function php_slug($string){

                        $spl_char = '/[^\-\s\pN\pL]+/u';
                        $double_char = '/[\-\s]+/';

                        $slug = preg_replace('/[^a-z0-9-]+/', '-', strtolower($string));
                        $slug = preg_replace($double_char, '-', $string);

                        $slug = trim($string, '-');

                        return $slug;
                    }

并包含在 HTACCESS 中

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^([^\.]+)$ $1.php [L,QSA]
#RewriteCond %{REQUEST_FILENAME} !-1

#RewriteRule ^/?$ /1 [QSA]

RewriteRule ^article([a-zA-Z0-9-/]+)$ article.php?article_link=$1 [L,QSA]
RewriteRule ^article([a-zA-Z0-9-/]+)/ article.php?article_link=$1 [L,QSA]

并链接为

<a href="article/?article_link='<?php echo $article_link;?>'">BTN</a>

【问题讨论】:

    标签: php .htaccess friendly-url slug


    【解决方案1】:

    请上移你的文章重写:

    RewriteEngine On
    
    RewriteRule ^article\/(.*)$ article.php?article_link=$1 [L]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteRule ^([^\.]+)$ $1.php [L,QSA]
    

    将您的 href 更改为 preety 链接(重写应该会捕获它们):

    <a href="article/<?php echo $article_link;?>">BTN</a>
    

    【讨论】:

    • 感谢您的帮助。此方法将我重定向到localhost/cloud-soft/article/index.php,错误消息是“此页面无法正常工作,将您重定向太多次”
    • 能否显示 BTN href 链接(在浏览器中呈现,而不是 php $article_link)?
    • 工作正常,但无法从文章页面获取内容。 ibb.co/VgWnSHk附截图,谢谢
    猜你喜欢
    • 2011-10-06
    • 2016-03-02
    • 2013-02-09
    • 2011-01-25
    • 1970-01-01
    • 2017-01-12
    • 1970-01-01
    • 2015-04-04
    • 2016-03-06
    相关资源
    最近更新 更多