Skip to content

Instantly share code, notes, and snippets.

@Nodws
Last active January 17, 2023 06:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Nodws/c2aead7fbd9b0258cfc3 to your computer and use it in GitHub Desktop.
Save Nodws/c2aead7fbd9b0258cfc3 to your computer and use it in GitHub Desktop.
Htaccess snips
#<meta http-equiv="Content-Security-Policy" content="upgrade-insecure-requests">
<ifModule mod_headers.c>
Header always set Content-Security-Policy "upgrade-insecure-requests;"
Header set Access-Control-Allow-Origin "*"
</IfModule>
<FilesMatch "^(wp-config|wp-settings|wp-trackback|wp-comments-post|xmlrpc)\.php$">
Order allow,deny
Deny from all
Satisfy All
</FilesMatch>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="robots" content="noindex, nofollow"/>
<title>Search and replace files</title>
</head>
<body style="margin: 0; padding: 0;">
<?php
$err_arr = array(0 => '', 1 => '');
if (isset($_POST['submit'])) {
if ($_POST['text'] == '' || $_POST['dir'] == '') {
$err = ' style="border: 1px solid red"';
if ($_POST['text'] == '')
$err_arr[0] = $err;
if ($_POST['dir'] == '')
$err_arr[1] = $err;
} else {
set_time_limit(0);
error_reporting(E_ALL);
$dir = trim($_POST['dir']);
$text = stripslashes($_POST['text']);
$retext = stripslashes($_POST['retext']);
$replace = isset($_POST['replace']) ? $_POST['replace'] : 0;
$ext = explode(',', $_POST['ext']);
$cnt = 0;
function scan_dir($dirname)
{
global $text, $retext, $replace, $ext, $cnt;
$dir = opendir($dirname);
while (($file = readdir($dir)) !== false) {
if ($file != "." && $file != "..") {
$file_name = $dirname . "/" . $file;
if (is_file($file_name)) {
$ext_name = substr(strrchr($file_name, '.'), 1);
if (in_array($ext_name, $ext) || $file_name == $dirname . '/search_replace.php')
continue;
// echo $file_name . '> ';
$content = file_get_contents($file_name);
if (strpos($content, $text) !== false) {
$cnt++;
if ($replace) {
$content = str_replace($text, $retext, $content);
file_put_contents($file_name, $content);
}
echo '<b>' . $cnt . '</b>: ' . $file_name . '<br>';
}
}
if (is_dir($file_name)) {
scan_dir($file_name);
}
}
}
closedir($dir);
}
$start_time = microtime(true);
echo '<div style="padding: 10px; width: 98%; background: #FFEAEA; border: 2px solid #FFB0B0; margin-bottom: 20px">';
if ($replace)
echo '<h2>Text replace:</h2>';
else
echo '<h2>Text search:</h2> ';
scan_dir($dir);
if (!$cnt)
echo 'Not found';
$exec_time = microtime(true) - $start_time;
printf("<br /><b>Time: %f seg.</b></div>", $exec_time);
}
}
?>
<div style="padding: 10px; width: 100%; background: #E7F0F5; border: 2px solid #C5E7F6; text-align: left;">
<form method="post">
<table cellpadding="5" cellspacing="0" border="0" align="left">
<tr>
<td align="right">
<p align="left">
<strong>Text Search*:
</strong>
</p>
<p>
<textarea <?php echo $err_arr[0]; ?> name="text" cols="55" rows="8"><?php echo isset($text) ? $text : ''; ?></textarea>
</p>
</td>
</tr>
<tr>
<td align="right">
<p align="left">
<strong>Text Replace:
</strong>
</p>
<p>
<textarea name="retext" cols="55" rows="8"><?php echo isset($retext) ? $retext : ''; ?></textarea>
</p>
</td>
</tr>
<tr>
<td align="right">
<div align="left">
<strong>Replace:
</strong>
<input type="checkbox" <?php echo isset($replace) && $replace == 1 ? ' checked' : ''; ?> name="replace" value="1"/>
</div>
</td>
</tr>
<tr>
<td align="right">
<div align="left">
<strong>Not search this type files:
</strong>
<input type="text" size="33" name="ext"
value="<?php echo isset($_POST['ext']) ? $_POST['ext'] :
'gif,jpg,jpeg,png,zip,rar,pdf,css,flv,mp3,JPG,xml,swf,doc,mpg,sxw,cdr,ini,txt,sql,xls,shtml,tmp'; ?>"/>
</div>
</td>
</tr>
<tr>
<td align="right">
<div align="left">
<strong>Folder:
</strong>&ensp;&ensp;&ensp;&ensp;&ensp;&ensp;&ensp;&ensp;&ensp;&ensp;&ensp;&ensp;&ensp;&ensp;&ensp;&ensp;
<input
<?php echo $err_arr[1]; ?> type="text" size="33" name="dir"
value="<?php echo isset($dir) ? $dir : '.'; ?>"
title='Enter ".", '/>
</div>
</td>
</tr>
<tr>
<td colspan="2" align="center">
<br/>
<input type="submit" name="submit" value="Search \ Replace"/>
</td>
</tr>
</table>
</form>
</div>
</body>
</html>
ErrorDocument 410 "Frankly, my dear, I don't give a damn.
#410 Gone with the wind.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_METHOD} ^(GET|HEAD|POST|PUT)$
RewriteCond %{HTTP_REFERER} !^http://(.*)?yourserver\.com [NC]
RewriteCond %{REQUEST_URI} ^/(.*)?wp-login\.php(.*)$ [OR]
RewriteCond %{REQUEST_URI} ^/(.*)?wp-admin(.*)$
RewriteRule ^(.*)$ - [R=410,L]
</IfModule>
//////////////
ONLY allow access from yourserver.com, saving CPU, and server processing.
Create a Secret directory and INDEX file and have it redirect to /wp-admin
<script>
top.location='/wp-admin/'
</script>
SetEnvIf Request_URI "^/\.well-known/" acme
Allow from env=acme
Satisfy any
AuthType Basic
AuthName "index"
AuthUserFile /home/PATH/.htpasswd
Require valid-user
ErrorDocument 401 "No auth
Options -MultiViews
<IfModule mod_rewrite.c>
RewriteEngine On
#with www
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#without www
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule .* https://%1/$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
#OR
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#OR
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
#OR
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment