[英]rewrite pretty urls with php and htaccess, mapping 404 for subpages
[英]Include fails when mapping pretty urls to actual urls (PHP/.htaccess)
第一次尝试创建漂亮的url,这是我的系统,.htaccess将所有内容映射回index.php,index.php将URL拆分为一个数组,然后使用case语句决定如何进行处理。
.htaccess:
RewriteBase /
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^.*$ ./index.php
index.php:
$request = $_SERVER['REQUEST_URI'];
#split the path by '/'
$params = explode("/", $request);
var_dump($params);
if(!isset($params[1]) || $params[1] == "") {
require_once('inc/content_default.php');
} else if ($params[1] == "posthandler"){
require_once('bin/posthandler.php');
} else {
switch($params[1]) {
case "dashboard":
include('inc/content_active.php?ign=' . $params[2]);
break;
}
}
问题是,当url为:[domain] / dashboard / AviateX14时,出现错误:
Warning: include() [function.include]: Failed opening '/inc/content_active.php?ign=AviateX14' for inclusion (include_path='.:/opt/php-5.3/pear') in /home/u502822836/public_html/index.php on line 92
该文件确实存在,代码存在问题,我知道这一点,因为我已经通过放入include('index.php')进行了测试,但它仍然失败。 该文件确实存在于inc / content_active.php中。
我在做错人吗? :(
编辑: $ params的var_dump是:
array(3) { [0]=> string(0) "" [1]=> string(9) "dashboard" [2]=> string(9) "AviateX14" }
您不能在php include()
拥有查询字符串,因为实际上并没有要求查询字符串。 您必须自己在服务器变量中填写参数:
switch($params[1]) {
case "dashboard":
$_GET['ign']=$params[2];
include('inc/content_active.php');
break;
}
您要让PHP查找名为以下文件: inc/content_active.php?ign=AviateX14
,其中包括?ign=AviateX14
部分。
解决此问题的一种方法是:
$_GET['ign'] = $params[2];
include( 'inc/content_active.php' );
但是,分配给$_GET
并不是一个很好的做法。 您可以做的更好的事情。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.