script to rewrite .htaccess file works on wamp but

2019-09-17 03:13发布

I have written a CMS that allows for control of the .htaccess file through administration. Everything is fine on my wamp on my computer and the updates occur and the .htaccess is rewritten as designed. However, on the a shared host on the net the script returns the update/rewrite page as a blank page with no source.

Viewing through the js console in chrome it flashes the following error which I was able to catch in print screen:

error

Though the update page is returned blank and the 500 error flashes the site itself is not affected and renders although an internal error has occurred.

My question... Is there perhaps a setting in php.ini or somewhere else on the server that I am not aware of that would prevent the .htaccess file from being dynamically updated?

My wamp is running PHP 5.5.12 while the server is 5.4

=== QUESTION UPDATE ===

The server is no recognizing \r and as a result the .htaccess file is rendering in a single line without link breaks and is commenting itself out.

Here is the original code:

# Prevent viewing of .htaccess file
    if($view_htaccess == 1){
    $htaccess_code .= "# Prevent viewing of .htaccess file \r";
    $htaccess_code .= "<Files .htaccess> \r";
    $htaccess_code .= "order allow,deny \r";
    $htaccess_code .= "deny from all \r";
    $htaccess_code .= "</Files> \r";
    $htaccess_code .= " \r";
    }

I tried \n\r and \r\n as well... not sure where to go from here

Thanks for any assistance in advance,

Pete

1条回答
ら.Afraid
2楼-- · 2019-09-17 04:02

ISSUE RESOLVED:

In wamp on my computer the server was not \n but did recognize \r so I programmed accordingly. Upon uploading to the server a 500 Error was caused by the server not recognizing \r or \n and running the code together where it was commenting itself out.

Although I do not why it worked the solution I found was to save the generated htaccess code to a temporary text file fist then call the contents of the temp text file through file_get_contents('htaccess-temp.txt'); and then save it to the htaccess file.

This however resulted in another issue where the script automatically was adding slashes to the slashes in the robot user agents Internet\ Ninja which required a str_replace("\\", "\\", $get_htaccess_code); to be applied to the return of file_get_contents('htaccess-temp.txt'); before saving to the htaccess file. The slash to replace with needed to be escaped.

The final code:

$file_handle = fopen('htaccess-temp.txt', 'w'); 
fwrite($file_handle, $htaccess_code); fclose($file_handle); 
$get_htaccess_code = file_get_contents('htaccess-temp.txt');    
$get_htaccess_code = str_replace("\\", "\\", $get_htaccess_code);
$file_handle = fopen($level.'.htaccess', 'w'); 
fwrite($file_handle, $get_htaccess_code); fclose($file_handle);

Though this works I still do not understand why I had to save the code to text first... and why slashes were being added. If you have any idea please comment.

Pete

查看更多
登录 后发表回答