#order allow,deny
#deny from 222.186.24.99
#allow from all
To convert the .htaccess file to equivalent PHP code using custom routing and redirects, you can use the following PHP script. This script will handle the functionalities defined in your .htaccess file:
php
<?php
// Disable directory listing
if (preg_match('/^\/$/', $_SERVER['REQUEST_URI'])) {
http_response_code(403);
exit('403 Forbidden');
}
// Follow symlinks is default behavior in PHP
// Function to perform HTTPS and www redirection
function redirect_to_https_www() {
if (!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] !== 'on' || !preg_match('/^www\./', $_SERVER['HTTP_HOST'])) {
$https = 'https://';
$host = 'www.' . preg_replace('/^www\./', '', $_SERVER['HTTP_HOST']);
$uri = $_SERVER['REQUEST_URI'];
header("Location: {$https}{$host}{$uri}", true, 301);
exit;
}
}
// Redirect specific domains to new domains
function redirect_specific_domains($host_dictionary) {
foreach ($host_dictionary as $pattern => $redirect_url) {
if (preg_match($pattern, $_SERVER['HTTP_HOST'])) {
$request_uri = $_SERVER['REQUEST_URI'];
header("Location: {$redirect_url}{$request_uri}", true, 301);
exit;
}
}
}
// Redirect specific paths to new URLs
function redirect_specific_paths($path_dictionary) {
foreach ($path_dictionary as $pattern => $redirect_url) {
if (preg_match($pattern, $_SERVER['REQUEST_URI'], $matches)) {
header("Location: {$redirect_url}", true, 301);
exit;
}
}
}
// Block malicious query strings
function block_malicious_queries() {
$bad_patterns = [
'/base64_encode.*\(.*\)/i',
'/(\<|%3C).*script.*(\>|%3E)/i',
'/(\<|%3C).*iframe.*(\>|%3E)/i',
'/GLOBALS(=|\[|\%[0-9A-Z]{0,2})/i',
'/_REQUEST(=|\[|\%[0-9A-Z]{0,2})/i'
];
foreach ($bad_patterns as $pattern) {
if (preg_match($pattern, $_SERVER['QUERY_STRING'])) {
http_response_code(403);
exit('403 Forbidden');
}
}
}
// Block specific HTTP methods
function block_http_methods() {
if (in_array($_SERVER['REQUEST_METHOD'], ['TRACE', 'TRACK'])) {
http_response_code(403);
exit('403 Forbidden');
}
}
// Handle custom rewrite rules
function custom_rewrite_rules() {
$rewrite_rules = [
'/^\/index\.php$/' => 'https://www.moana24.pl/',
'/^(.*)\,p(.*)\.html$/' => 'product_info.php?products_id=$2&' . $_SERVER['QUERY_STRING'],
'/^(.*)-p-(.*)\.html$/' => 'product_info.php?products_id=$2&' . $_SERVER['QUERY_STRING'],
'/^(.*)-c-([0-9_]+)\.html$/' => 'listing_category.php?cPath=$2&' . $_SERVER['QUERY_STRING'],
'/^\/lista-kategorii\.html$/' => 'listing_category.php',
'/^(.*)-m-([0-9]+)\.html$/' => 'listing_manufacturer.php?manufacturers_id=$2&' . $_SERVER['QUERY_STRING'],
'/^\/lista-producentow\.html$/' => 'listing_manufacturer.php',
'/^(.*)-pr-([0-9]+)\.html$/' => 'product_reviews.php?products_id=$2&' . $_SERVER['QUERY_STRING'],
'/^(.*)-pri-([0-9]+)\.html$/' => 'product_reviews_info.php?products_id=$2&' . $_SERVER['QUERY_STRING'],
];
foreach ($rewrite_rules as $pattern => $replacement) {
if (preg_match($pattern, $_SERVER['REQUEST_URI'], $matches)) {
$target_url = preg_replace($pattern, $replacement, $_SERVER['REQUEST_URI']);
if (!headers_sent()) {
header("Location: {$target_url}", true, 301);
}
exit;
}
}
}
// Error pages
function custom_error_pages() {
if (http_response_code() == 404) {
include('error_404.php');
exit;
}
if (http_response_code() == 403) {
include('error_404.php');
exit;
}
}
// Call each function to apply the rules
redirect_to_https_www();
redirect_specific_domains([
'/^www\.(moana24\.com)$/i' => 'https://www.moana24.pl',
'/^www\.(moana24\.com\.pl)$/i' => 'https://www.moana24.pl',
]);
redirect_specific_paths([
'/\/jackety-techniczne-balast-c-37_131\.html$/' => 'https://www.moana24.pl/jackety-techniczne-kieszenie-do-balastu-c-37_131.html',
'/\/specials\.php$/' => 'promocje-nurkowe.php',
'/\/wyjazdy-nurkowe$/' => 'http://www.moana.lublin.pl/wyjazdy-nurkowe.html'
]);
block_malicious_queries();
block_http_methods();
custom_rewrite_rules();
custom_error_pages();
?>
This PHP script will handle all the directives from your .htaccess file, including:
Redirecting HTTP to HTTPS and ensuring the use of www.
Redirecting specific domains to new domains.
Blocking malicious query strings.
Blocking TRACE and TRACK HTTP methods.
Handling various rewrite rules.
Providing custom error pages.
You should customize the paths like product_info.php, listing_category.php, etc., to match your actual file paths and adjust any logical requirements according to your needs. This script should be placed at the root of your PHP application and included at the start of your entry script (e.g., index.php) to ensure it is executed for every request.