我尝试使用.htaccess来制作干净的URL。但我在多个查询字符串参数中遇到问题。
我有三种这样的网址格式:
1. http://localhost/websekolah/main.php?page=register
2. http://localhost/websekolah/main.php?page=detail_news&uid=10
3. http://localhost/websekolah/main.php?page=gallery&hal=3
我想要这样的网址:
. 1. http://localhost/websekolah/register
2. http://localhost/websekolah/detail_news/10
3. http://localhost/websekolah/gallery/hal/3
我创建了这个.htaccess但只适用于1个查询字符串
RewriteEngine On
RewriteRule ^[css|images|js|config|lib|pages] - [L,NC]
RewriteRule ^(.*)$ main.php?page=$1 [L,QSA]
我在main.php中检查我的网址重定向:
<?php
session_start();
if(isset($_GET['page'])) {
$page = $_GET['page'];
$_SESSION['curr_page'] = $page;
if(isset($_GET['process'])) {
$process = $_GET['process'];
if(file_exists("pages/process_$process.php")) {
include "pages/process_$process.php";
} else {
include "pages/404.php";
}
} else {
if(file_exists("pages/body_$page.php")) {
include "pages/body_$page.php";
} else {
include "pages/404.php";
}
}
} else {
include "pages/body_home.php";
}
?>
请帮助,谢谢:)
答案 0 :(得分:2)
尝试更改
RewriteRule ^(。*)$ main.php?page = $ 1 [L,QSA]
要
RewriteRule ^(。*)$ main.php [L,QSA]
查询参数仍在$_GET
答案 1 :(得分:2)
使用此:
RewriteRule ^([^/]+)/?$ main.php?page=$1 [L,QSA]
RewriteRule ^([^/]+)/([^/]+)/?$ main.php?page=$1&uid=$2 [L,QSA]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ main.php?page=$1&$2=$3 [L,QSA]
RewriteRule ^$ main.php [L,QSA]