Php 使用数据库数据重写/重定向Apache URL

Php 使用数据库数据重写/重定向Apache URL,php,apache,.htaccess,mod-rewrite,redirect,Php,Apache,.htaccess,Mod Rewrite,Redirect,这是我的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代码(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>
在qs1.php文件中,我通过
$\u GET['var1']
获取查询字符串
var1

我希望链接可以通过此url访问
http://localhost/qs1/12345678
。 如果用户像这样在末尾加斜杠
http://localhost/qs1/12345678/
然后页面重定向到此
http://localhost/qs1/12345678

目前,在打开此
http://localhost/qs1/12345678/


感谢您的评论。

您的规则不匹配,因为它在数字之后等待其他内容
e、 g.
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]

您的规则不匹配,因为它在数字后等待其他内容
e、 g.
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]

您的规则不匹配,因为它在数字后等待其他内容
e、 g.
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]

您的规则不匹配,因为它在数字后等待其他内容
e、 g.
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]

@贾斯汀勒曼一直打扰你看起来不太好。我问了一个问题,但还没有人回答@贾斯汀勒曼一直打扰你看起来不太好。我问了一个问题,但还没有人回答@贾斯汀勒曼一直打扰你看起来不太好。我问了一个问题,但还没有人回答@贾斯汀勒曼一直打扰你看起来不太好。我问了一个问题,但还没有人回答。