【问题标题】:url rewrite to query string and includes conflicturl 重写为查询字符串并包含冲突
【发布时间】:2018-02-11 13:47:38
【问题描述】:

在寻找解决方案几天后,我认为除了展示整个案例之外别无选择。我有一个站点,该站点通过依赖于 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 设置为忽略资产文件

&lt;base href="http://localhost/site-bewerbung/site/"&gt; 在 html 中,
href="./css/style.css" 为 CSS 添加 ./ 并包含所有内容。

现在该网站正在重新整合。资产加载,但现在查询字符串似乎不再有效。 company/*.php 中的所有文本内容都消失了,我完全陷入困境。

感谢您的建议!

【问题讨论】:

  • 尝试重新排序您的规则。将第二个规则放在第一个规则之上,并在第一个 RewriteRule 中添加 [L] 标志
  • 我实际上只是把它们按错误的顺序放在这里,所以什么也没做。旗帜也没有。

标签: php .htaccess url mod-rewrite include


【解决方案1】:

你可以使用一个简单的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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-08-02
    • 2012-01-18
    • 1970-01-01
    • 1970-01-01
    • 2011-05-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多