Home url rewrite to query string and includes conflict
Reply: 1

url rewrite to query string and includes conflict

Malte Podolski
1#
Malte Podolski Published in 2018-02-11 13:47:38Z

After looking for a solution for days I see no other option than to present this whole case. I have a site that includes the text content via an array that depends to a query string on the URL. The site structure is this

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

The index file looks somewhat like this:

// 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"]; ?>

This works just fine as long as I use the query-string:
http://localhost/site-bewerbung/site/index.php?visitor=miavilla

By setting a rewrite rule I want to provide a clean URL like
http://localhost/site-bewerbung/miavilla/

The .htaccess:

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

I then had the problem that all CSS and Images were lost. After researching I set htaccess to ignore asset files by adding

<base href="http://localhost/site-bewerbung/site/"> in the html and
href="./css/style.css" adding ./ for CSS and all includes.

Now the site is coming back together. The assets load but now the Query-string seems to have no effect anymore. All the text content from the companies/*.php are gone and I'm completely stuck.

Thanks for your advice!

S. Haebich
2#
S. Haebich Reply to 2018-02-17 10:45:37Z

You can use a simple url router array

$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;
}
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.297997 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO