【问题标题】:Apache URL rewrite/redirection with database data使用数据库数据重写/重定向 Apache URL
【发布时间】:2015-07-31 05:59:06
【问题描述】:

这是我的 php 代码(qs.php)。该文件包含漂亮的 url 链接。

<html>
<head></head>
<body>
    <?php  
    $file = 'qs1'; //this is a php file qs1.php
    $id = '12345678'; //testing ID
    $complete_url = $file."/".$id;

    ?>
    <a href ="<?php echo $complete_url;?>"> This is a test link </a>

</body></html>

此链接出现链接此-http://localhost/qs1/12345678

QS1 是一个 php 文件 (qs1.php)。

以下是目前的 htaccess 文件。

RewriteEngine On
RewriteBase /

RewriteRule ^([^/]+)/(\d+)/([^/]+)$ $qs1.php?var1=$2 [NC,L]

在 qs1.php 文件中,我通过 $_GET['var1'] 获取查询字符串 var1

我希望可以通过此 url http://localhost/qs1/12345678 访问链接。 如果用户像这样http://localhost/qs1/12345678/在末尾加上斜线,那么页面会将自己重定向到这个http://localhost/qs1/12345678

目前我在打开此 http://localhost/qs1/12345678/ 时收到错误 404

感谢您的评论。

【问题讨论】:

    标签: php apache .htaccess mod-rewrite redirect


    【解决方案1】:

    您的规则不匹配,因为它在数字后等待其他内容
    例如http://example.com/qs1/12345678/something

    您可以在 htaccess 中使用此代码

    Options -MultiViews
    
    RewriteEngine On
    RewriteBase /
    
    # we don't touch existing files/folders
    RewriteCond %{REQUEST_FILENAME} -d [OR]
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule ^ - [L]
    
    # remove trailing slash (for instance: redirect /qs1/12345678/ to /qs1/12345678)
    RewriteRule ^([^/]+)/(\d+)/$ $1/$2 [R=301,L]
    
    # rewrite /xxx/12345 to /xxx.php?var1=12345 (if xxx.php exists)
    RewriteCond %{DOCUMENT_ROOT}/$1\.php -f
    RewriteRule ^([^/]+)/(\d+)$ $1.php?var1=$2 [L]
    
    # rewrite /xxx/12345/something to /xxx.php?var1=12345&var2=something (if xxx.php exists)
    RewriteCond %{DOCUMENT_ROOT}/$1\.php -f
    RewriteRule ^([^/]+)/(\d+)/([^/]+)$ $1.php?var1=$2&var2=$3 [L]
    

    【讨论】:

    猜你喜欢
    • 2015-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-12
    • 2012-10-09
    相关资源
    最近更新 更多