【问题标题】:How to implement Apache Rewrite Rule correctly?如何正确实现 Apache Rewrite Rule?
【发布时间】:2017-06-24 08:29:51
【问题描述】:

以下是我写在.htaccess文件中的重写规则代码放在

beta1

文件夹。

Options +FollowSymlinks
RewriteEngine on
RewriteBase /beta1/
RewriteRule /(.*) read.php?post=$1 [R,NC]

目前的网址是:

http://localhost/beta1/read.php?post=Happy+Mother+Day.

我希望链接看起来像:

http://localhost/beta1/Happy-Mother-Day.

我是否还需要更改“read.php”从数据库中提取数据的方式?如果是,那么如何? 我还需要在“read.php”文件中进行任何更改吗?

上面的重写代码不起作用。

【问题讨论】:

    标签: php apache .htaccess mod-rewrite


    【解决方案1】:

    嘿,请试试这个规则

     RewriteEngine On    # Turn on the rewriting engine
    
     RewriteRule    ^([A-Za-z0-9-_]+)?$    read.php?post=$1    [NC,L]  
    

    并读取.php文件

      echo str_replace('-',' ', $_GET['post']); 
    

    【讨论】:

    • 如何查看修改是否生效?我是否还需要在 read.php 中更改/添加任何内容?
    • 您将此代码替换为 .htaccess 文件中的代码。在 read.php 文件中,您必须获取 $_GET 数据。
    • 您检查localhost/beta1/Happy-Mother-Day 并从 read.php 文件中获得结果吗?
    • 我用 $_GET['post']; 得到了帖子的标题;然后对其进行清理,然后从 mysql 中检索数据。
    • 请用您想要的 read.php 文件的代码添加您的问题。
    猜你喜欢
    • 1970-01-01
    • 2012-04-18
    • 2018-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-27
    相关资源
    最近更新 更多