Skip to content

Commit

Permalink
update htaccess rewrite rules
Browse files Browse the repository at this point in the history
.htaccess rules were causing a redirect loop.
  • Loading branch information
edsonsantoro authored Apr 8, 2024
1 parent f9b8243 commit 148ca0d
Showing 1 changed file with 53 additions and 29 deletions.
82 changes: 53 additions & 29 deletions admin/partials/wpmu-client-admin-redirects.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,43 +250,67 @@ public function delete_redirect() {
* @return void
*
*/
public function build_htaccess( string $status ) {
if ( $status != "success" ) {
public function build_htaccess(string $status) {
// If status is not "success", exit function
if ($status !== "success") {
return;
}


// Get blog settings slug
$blog_settings_slug = $this->get_blog_settings_slug();

$https = '';
$directory = get_option( $blog_settings_slug . '_export_path', '' );

if ( empty ( $directory ) )
return;

$directory = realpath( $directory );
$htaccessFile = $directory . "/.htaccess";

$redirects = get_option( $blog_settings_slug . "_redirects", [] );
$force_https = get_option( $blog_settings_slug . "_force_https", false );

if ( $force_https ) {
$https = "RewriteCond %{HTTPS} !=on\nRewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]\nRewriteEngine On\n";

// Get export directory
$export_path = get_option($blog_settings_slug . '_export_path', '');

// If export directory is empty, exit function
if (empty($export_path)) {
return;
}

$https .= "RewriteEngine On\nRewriteBase /\nRewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]\nRewriteRule ^(.*)$ https://%1/$1 [R=301,L]\n\n";
$https .= "<IfModule mod_headers.c>\n<FilesMatch \"\.(js|css|woff|woff2|eot|ttf|png|jpg|svg|ico|jpeg|webp)$\">\nHeader set Cache-Control \"max-age=31536000, public\"\n</FilesMatch>\n</IfModule>";

if ( $fileHandle = fopen( $htaccessFile, 'w' ) ) {
fwrite( $fileHandle, $https );
foreach ( $redirects as $source => $destination ) {
$redirectRule = "Redirect 301 $source $destination\n";
fwrite( $fileHandle, $redirectRule );
}
fclose( $fileHandle );

// Resolve export directory path
$export_path = realpath($export_path);
$htaccess_file = $export_path . "/.htaccess";

// Get redirects and force HTTPS option
$redirects = get_option($blog_settings_slug . "_redirects", []);
$force_https = get_option($blog_settings_slug . "_force_https", false);

// Initialize HTTPS rules
$https_rules = '';

// Add HTTPS redirection rule if force HTTPS is enabled
if ($force_https) {
$https_rules .= "RewriteCond %{HTTPS} !=on\n";
$https_rules .= "RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]\n";
$https_rules .= "RewriteEngine On\n";
}

// Add www to non-www redirection rule
$https_rules .= "RewriteEngine On\n";
$https_rules .= "RewriteBase /\n";
$https_rules .= "RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]\n";
$https_rules .= "RewriteRule ^(.*)$ https://%1/$1 [R=301,L]\n\n";

// Add cache control headers for certain file types
$https_rules .= "<IfModule mod_headers.c>\n";
$https_rules .= "<FilesMatch \"\.(js|css|woff|woff2|eot|ttf|png|jpg|svg|ico|jpeg|webp)$\">\n";
$https_rules .= "Header set Cache-Control \"max-age=31536000, public\"\n";
$https_rules .= "</FilesMatch>\n";
$https_rules .= "</IfModule>";

// Write rules to .htaccess file
if ($file_handle = fopen($htaccess_file, 'w')) {
fwrite($file_handle, $https_rules);
foreach ($redirects as $source => $destination) {
$redirect_rule = "Redirect 301 $source $destination\n";
fwrite($file_handle, $redirect_rule);
}
fclose($file_handle);
}
}



/**
* Function to create a section info
*
Expand Down

0 comments on commit 148ca0d

Please sign in to comment.