【问题标题】:PHP rewrite rule as database values [closed]PHP将规则重写为数据库值[关闭]
【发布时间】:2013-10-17 13:02:22
【问题描述】:

我需要重写 www.example.com/folder/35467/title.html

www.example.com/folder/?id=35467

idtitle 来自数据库。我要把 htaccess 文件放到“文件夹”中。

【问题讨论】:

  • 那么这里的实际问题是什么?你试过吗?谷歌搜索 URL 重写?
  • 我已经使用重写但不作为数据库中的值
  • 但是您仍然没有提供实际问题。你想达到什么目的
  • 我需要将 ?id= 替换为 p 或任何内容,并从数据库中添加 title 值,其中 title 与被质疑的 id 相关联
  • 链接是链接..你可以用 htaccess 操作它们.. DB 与它没有任何关系..

标签: php database .htaccess url-rewriting


【解决方案1】:

如果 htaccess 文件将在 /folder/ 中,那么您需要以下规则:

RewriteEngine On
RewriteBase /folder/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9]+)/(.*)\.html ?id=$1 [L]

要使用 id 查询字符串重定向访问,您可以检查 index.php 中的 $_SERVER['REQUEST_URI'] 变量。

if ( !strpos($_SERVER['REQUEST_URI'],'.html') ) {
    // The request that was made wasn't with the nicer looking URL
    header("Location: /folder/$id/$title.html");
    exit();
}

【讨论】:

    【解决方案2】:

    .htaccess

    <IfModule mod_rewrite.c>
    
        Options +FollowSymLinks
        RewriteEngine on
    
    ############################################
    ## you can put here your script root folder
    ## path relative to web root
    
        RewriteBase /
    
    ############################################
    ## never rewrite for existing files, directories and links
    
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_FILENAME} !-l
    
    ############################################
    ## rewrite everything else to index.php
    
        RewriteRule .* index.php [L]
    
    </IfModule>
    

    index.php

    list (,,$id,$title) = explode('/', $_SERVER['REQUEST_URI']);
    $link = 'www.example.com/folder/?id='. $id;
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-02-13
    • 2016-06-20
    • 2021-04-04
    • 1970-01-01
    • 1970-01-01
    • 2018-10-06
    • 2011-04-11
    • 1970-01-01
    相关资源
    最近更新 更多