Apache URL重写/重定向与数据库数据


Apache URL rewrite/redirection with database data

这是我的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文件我得到查询字符串var1$_GET['var1']

我想链接可以通过这个url http://localhost/qs1/12345678访问。如果用户在http://localhost/qs1/12345678/后面加上斜杠,那么页面将重定向到http://localhost/qs1/12345678

目前我得到错误404,而打开这个http://localhost/qs1/12345678/

谢谢你的评论

你的规则不匹配,因为它等待数字之后的其他内容
例: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]