Using PHP Variables in .htaccess

My solution to using PHP variables in the .htaccess file was to create a htaccess.php file which would write the .htaccess file whenever needed.

htaccess.php

<?php

	/**
	 * @file
	 *
	 * Creates and replaces the .htaccess file with the data loaded from the config file.
	 */

	include_once( dirname(__FILE__) . "/config.php" );

	$strData = '# Do not manually edit this file; this file is generated by htaccess.php
	RewriteEngine on
	ErrorDocument 404 /index.php
	# Block directory listing
	Options -Indexes
	# Game
	RewriteRule ^' . GAME_FOLDER_NAME . '/([_A-Za-z0-9-]+)(/)?$ game.php?strPage=$1
	';

	// Remove tabs.
	$strData = str_replace( "\t", "", $strData );
	// Create the .htaccess file.
	file_put_contents( ".htaccess", $strData );

Leave a Reply

Your email address will not be published. Required fields are marked *