url重写为查询字符串并包含冲突

时间:2018-02-11 13:47:38

标签: php .htaccess url mod-rewrite include

在寻找解决方案几天之后,除了提出整个案例之外别无其他选择。我有一个网站,通过一个数组包含文本内容,该数组依赖于URL上的查询字符串。 网站结构是这个

site-bewerbung/  
 .htaccess  
 site/  
 index.php  
 functions.php   
  companies/  
   *.php  
  css/  
   fonts/  
  images/  
  includes/

索引文件看起来像这样:

// includes the text content from the php-files in the companies-
// directory which are arrays
include('./companies/miavilla.php');
include('./companies/eurolight.php');

include('./functions.php');
// which contains
$company = $_GET['visitor'];
// to get the contents dynamically depending on which url is used

// In the <body> I get larger parts of the site as includes from the
// includes-directory like 
<?php include('./includes/section-anschreiben.php'); ?>  

// and the text contents from the files in the companies-directory
// are pulled in like this 
<?php echo $companies[$company]["job"]; ?>

只要我使用查询字符串,这就可以正常工作:
http://localhost/site-bewerbung/site/index.php?visitor=miavilla

通过设置重写规则,我想提供一个干净的URL,如
http://localhost/site-bewerbung/miavilla/

.htaccess:

RewriteRule \.(css|jpe?g|gif|png|js|ico|woff|woff2)$ - [L]
RewriteRule ^(.+)$ site/index.php?visitor=$1 [L]

然后我遇到了所有CSS和图像丢失的问题。在研究之后,我通过添加

来设置htaccess来忽略资产文件 html和中的

<base href="http://localhost/site-bewerbung/site/"> href="./css/style.css"为CSS添加./和所有包含。

现在网站又回来了。资产加载但现在查询字符串似乎不再有效。来自公司/ * .php的所有文本内容都消失了,我完全卡住了。

感谢您的建议!

1 个答案:

答案 0 :(得分:0)

您可以使用简单的url路由器阵列

$url = $_SERVER['REQUEST_URI'];
$internalId = 0;
$seoRouter = [
    'your-seo-url' => 1
];

if (array_key_exists[$url]) {
    $internalId = $seoRouter[$url];
}

switch ($internalId) {
    case: 1
        //include php file here
        break;
    case: 2
        //include file here
        break;
    case default:
        //i.E. 404 not found here
        break;
}