permlink htaccess is not being generated properly
-
I am hosting couple wordpress “sites” as subfolders in my server:
https://www.ngtech.co.il/books/
The path is in hebrew and is hosted on an Oracle Linux Server 8.x and was the issue was tested on Debian 11+12, Ubuntu 20.04+22.04 and Almalinux+RockyLinux+RHEL+OracleLinux 8+9.
The issue is that the .htaccess that is being generated by wordpress doesn’t work for a redirection of the links.
IE the content of the auto generated .htaccess file is:BEGIN WordPress ??????? (?????) ??? “BEGIN WordPress” ???? “END WordPress” ?? ???? ????? ?????, ??? ????? ???? ?? ??????? ??????? ?? ???????. ?? ????? ??????? ??? ?????? ??? ????.
# BEGIN WordPress # ??????? (?????) ??? "BEGIN WordPress" ???? "END WordPress" ?? # ???? ????? ?????, ??? ????? ???? ?? ??????? ??????? ?? ???????. # ?? ????? ??????? ??? ?????? ??? ????. <IfModule mod_rewrite.c> RewriteEngine On RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}] RewriteBase /books/%D7%A9%D7%94%D7%99%D7%90%D7%95%D7%A9-%D7%95%D7%94%D7%98%D7%A8%D7%92%D7%93%D7%99%D7%94-%D7%A0%D7%A4%D7%92%D7%A9%D7%99%D7%9D/ RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /books/%D7%A9%D7%94%D7%99%D7%90%D7%95%D7%A9-%D7%95%D7%94%D7%98%D7%A8%D7%92%D7%93%D7%99%D7%94-%D7%A0%D7%A4%D7%92%D7%A9%D7%99%D7%9D/index.php [L] </IfModule> # END WordPress
While this doesn’t work.
So what I have done is to change the htaccess to the next on Oracle Linux 8 and Alma linux 8+9:# BEGIN WordPress # ??????? (?????) ??? "BEGIN WordPress" ???? "END WordPress" ?? # ???? ????? ?????, ??? ????? ???? ?? ??????? ??????? ?? ???????. # ?? ????? ??????? ??? ?????? ??? ????. <IfModule mod_rewrite.c> RewriteEngine On RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}] RewriteBase /books/??????-????????-??????/ RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /books/??????-????????-??????/index.php [L] </IfModule> # END WordPress
I don’t know the exact technical reasons and what causes wordpress to generate faulty .htaccess rules but at-least I have a solution now.
I have used the next php code to generate the right .htaccess file content:
<?php // Define the opening and closing comments to identify the section in .htaccess $openingComment = "# BEGIN Custom .htaccess Configuration"; $closingComment = "# END Custom .htaccess Configuration"; $requestPath = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH); $scriptName = pathinfo($_SERVER['SCRIPT_NAME'], PATHINFO_BASENAME); $requestPath = str_replace('/' . $scriptName, '', $requestPath); // Decode the percent-encoded characters in the request path $requestPath = urldecode($requestPath); // Define the new content to replace the section between the comments $newContent = " # BEGIN Custom .htaccess Configuration # Enable Rewrite Engine RewriteEngine On # Set HTTP_AUTHORIZATION environment variable RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}] # RewriteBase for the dynamic path RewriteBase $requestPath # Rewrite rule for clean URLs RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . $requestPath/index.php [L] # END Custom .htaccess Configuration "; // Get the path to the .htaccess file in the current directory $htaccessFilePath = __DIR__ . '/.htaccess.in'; // Check if the .htaccess file exists if (file_exists($htaccessFilePath)) { // Read the existing .htaccess content $currentContent = file_get_contents($htaccessFilePath); // Find the position of the opening and closing comments $openingPos = strpos($currentContent, $openingComment); $closingPos = strpos($currentContent, $closingComment); // Replace the section between the comments with the new content if ($openingPos !== false && $closingPos !== false) { $currentContent = substr_replace($currentContent, $newContent, $openingPos, $closingPos - $openingPos + strlen($closingComment)); } else { echo "Failed to find the specified comments in the .htaccess file."; } } else { // If the file doesn't exist, create it with the new content $currentContent = $newContent; } // Write the updated or new content to the .htaccess file if (file_put_contents($htaccessFilePath, $currentContent)) { echo "The .htaccess.in file has been updated or created in the current directory."; } else { echo "Failed to update or create the .htaccess.in file."; } ?>
Who can help with resolving this issue and testing it on couple OS’s and distros?
- The topic ‘permlink htaccess is not being generated properly’ is closed to new replies.