我正在尝试为 seo url 创建 htaccess,我创建了 htaccess 所有 url 都工作正常,排除带参数的子文件夹,它们重定向到站点文件夹。
sitefolder 是我的根目录,所有文件都在那里。
示例:网址应如下所示:
http://localhost/sitefolder/subcat/39/web-hosting http://localhost/sitefolder/profile/1/username
在 htaccess 中它是这样的:
RewriteRule ^subcat/(.*)/([a-zA-Z0-9_-]+)$ subcat.php?q=$1&slug=$2 [L,NC] RewriteRule ^profile/(.*)/([a-zA-Z0-9_-]+)$ show_profile.php?q=$1&slug=$2 [L,NC]
第一个参数是 id,第二个参数是 slug,但这两个 url 都不会转到 url。
这是我所有的 htacces 代码:
Options +FollowSymLinks -MultiViews RewriteEngine on RewriteRule ^contact-us contact-us.php [L,NC] RewriteRule ^login login.php [L,NC] RewriteRule ^register register.php [L,NC] RewriteRule ^search search.php [L,NC] RewriteRule ^about-us about-us.php [L,NC] RewriteRule ^(.*)/([a-zA-Z0-9_-]+)$ detail.php?q=$1&slug=$2 [L,NC] RewriteRule ^cats categories.php [L,NC] RewriteRule ^subcat/(.*)/([a-zA-Z0-9_-]+)$ subcat.php?q=$1&slug=$2 [L,NC] RewriteRule ^profile/(.*)/([a-zA-Z0-9_-]+)$ show_profile.php?q=$1&slug=$2 [L,NC]
这就是我在 subcat.php 中获取参数的方式,我不会在错误时重定向到索引,所以我不认为这是因为 php :
if(!empty($_GET['slug']) AND !empty($_GET['q'])) {
$slug = $_GET['slug'];
$pid = is_numeric($_GET['q']);
$userQuery = $handler->getPosts("SELECT * FROM categories WHERE catId = ?", [$pid])->fetchAll();
if($userQuery) {
foreach($userQuery as $user){
$catid = intval($user['catId']);
$cat_title = htmlspecialchars($user['catName']);
$cat_seo_url = htmlspecialchars($user['cat_seo_url']);
$cat_descriptions = htmlspecialchars($user['cat_desc']);
$cat_keywords = htmlspecialchars($user['cat_keys']);
$cat_created = htmlspecialchars($user['cat_created_at']);
$cat_img = htmlspecialchars($user['cat_img']);
}
}else{
echo "Something went wrong while execute query.";
exit();
}
}else{
echo "Something went wrong with parameters";
exit();
} Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号