43947

How to run shtml include in php

Question:

I have dozens of .shtml files on the server which include this statement to include .inc files:

<!--#include virtual="../v4/rightmenu.inc" -->

This is exactly how it shows on the source which is working just fine.

I am wondering whether I can run this on my php code without changing it since the current files has this kind of inclusion a lot and I don't want to mess with a lot of code like this one.

I just don't want to change it to something like <?php include "../v4/rightmenu.inc"; ?>

Answer1:

Use <a href="http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html" rel="nofollow">mod_rewrite</a> to route all requests to .shtml files through a single php file. For exapmle: _includeParser.php

A very rough sketch of such a file could be:

/** * This function should return the contents of the included file */ function getIncludeFileContents($match) { list (, $file) = $match; // return something for $file } /** * This function should return a php-readable path for the initially requested .shtml file */ function getRealpath($uri) { // return the real path of the requested uri } // Map the requested uri to file // 'REDIRECT_URL' could be something different for your configuration, have a look at the $_SERVER array // to get the correct value if something fails here $realpath = getRealpath($_SERVER['REDIRECT_URL']); // parse each include statement // the regex pattern here could need some tweaking $output = preg_replace_callback( '@<!--#include virtual="(.+?)" -->@i', 'getIncludeFileContents', file_get_contents($realpath) ); // output the final contents echo $output;

Of course a solution like this could be optimized by some caching or other methods.

Recommend

  • Python - Convenient way to check for a value in a list that is part of a dictionary?
  • Selecting elements with lodash where inner properties match values in an array
  • Can I configure Jackson JSON pretty printing from annotations or from Spring MVC controller?
  • Does a SQL Server Computed Column require “persistence” in order for it to be indexed efficiently?
  • Create dynamic subdirectories using htaccess and php
  • Mod_rewrite: Include query string when redirecting
  • (Un)setting a cookie in mod_rewrite
  • Parsing Java String to date
  • Too many Redirects after switching WordPress to https
  • Accessing environment variables set using mod_rewrite in PHP
  • jQuery.fn.load() is deprecated?
  • Can I used mod_rewrite to change file extensions? .jpeg to .jpg for example
  • JAXB 2 in an Oracle 10g Webapp
  • htaccess will not rewrite subdomains
  • Inno Setup Search for specifc file on a CD, retrieve exact filepath and return value to [Files]-Sect
  • Removing the .php file extension from the URL Request
  • Regex for Specific Tag
  • Primefaces lazy datascroller calling load twice
  • How can we prepend rows to a react native list-view?
  • How to access meteor package name inside package?
  • NHibernate manually control fetching
  • Rest Services conventions
  • SonarQube: Cannot deactivate rule with missing quality profile
  • What's the purpose of QString?
  • Jackson Parser: ignore deserializing for type mismatch
  • Yii2: Config params vs. const/define
  • Ajax Loaded meta Tags
  • Calling of Constructors in a Java
  • Invalid access key error using credentials redeemed from an amazon open id token
  • Traverse Array and Display in markup
  • Transpose CSV data with awk (pivot transformation)
  • PHP: When would you need the self:: keyword?
  • KeystoneJS: Relationships in Admin UI not updating
  • Acquiring multiple attributes from .xml file in c#
  • Why can't I rebase on to an ancestor of source changesets if on a different branch?
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • How can I remove ASP.NET Designer.cs files?
  • reshape alternating columns in less time and using less memory
  • java string with new operator and a literal
  • How can I use threading to 'tick' a timer to be accessed by other threads?