下面是我的 php 代码 (qs.php)。此文件包含 url 链接。使用 SLUG 创建漂亮的链接。
<html>
<head></head>
<body>
<?php
function create_slug($string){
$slug=preg_replace('/[^A-Za-z0-9-]+/', '-', $string);
$slug = strtolower($slug);
return $slug;
}
$slugurl= create_slug('SLUG-text-testing-purpose-only');
$file = 'qs1'; //this is a php file qs1.php
$id = '12345678'; //testing ID
$complete_url = $file."/".$id."/".$slugurl;
?>
<a href ="<?php echo $full;?>"> This is a test link created by slug</a>
</body></html>
上面的链接是这样的 - http://localhost/qs/qs1/12345678/slug-text-testing-purpose-only
编辑
QS是放置所有文件的目录。 QS1是一个php文件(qs1.php)
我想从上面的链接 12345678 & slug-text-testing-purpose-only
获取两个变量。
因为 qs1.php 是我从 $_GET['var1'] & $_GET['var2'];
我知道可以通过.htaccess
来实现。我尝试了多种选择,但它们都不起作用,因为我收到 404。
.htaccess文件放在QS目录下
以下是我尝试过的选项。
RewriteEngine On
RewriteRule ^qs/([0-9]+)/([^/]+)$ qs1.php?var1=$1&var2=$2 [L]
RewriteRule ^/([^/]*)/([^/]*)$ /qs1.php?var1=$1&var2=$2 [L]
RewriteRule ^qs/([^/]+)/([^/]+)/?$ /qs1.php?var1=$1&var2=$2 [QSA,L]
RewriteRule /qs/(\d+)/(.*) qs1.php?id=$1&slug=$2 [QSA,L]
我对 htaccess 和 url 重定向还很陌生。
请指教。
谢谢
最佳答案
你可以把这段代码放在/qs/.htaccess
RewriteEngine On
RewriteBase /qs/
RewriteRule ^([^/]+)/(\d+)/([^/]+)$ $1.php?var1=$2&var2=$3 [NC,L]
现在,您可以访问 http://localhost/qs/qs1/12345678/slug-text-testing-purpose-only
,您将获得与 /相同的内容qs/qs1.php?var1=12345678&var2=slug-text-testing-purpose-only
关于php - URL 重写(SLUG)和 URL 重定向(htaccess),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30280098/