I'm wondering if there's a way to load text string into my htaccess file. I don't want to manually type it in the file. Here's what I already have
<IfModule mod_rewrite.c>
RewriteBase /file_name.txt/
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^gwcgroup\.co.uk [OR]
RewriteCond %{HTTP_HOST} ^www\.gwcgroup\.co.uk$
RewriteRule .* https://gwcgroup.co.uk%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTPS} =on
RewriteCond %{HTTP_HOST} ^www\.gwcgroup\.co.uk$
RewriteRule .* https://gwcgroup.co.uk%{REQUEST_URI} [R=301,L]
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
Any suggestion would be appreciated. thanks in advance
CodePudding user response:
This feels like an X-Y problem.
(Warning... lots of "however"s follow...)
Yes, you can read a text file into a .htaccess file using mod_rewrite and an Apache expression (Apache 2.4). However, you can't read it directly into the RewriteBase directive as you are implying. However, you can read the file and store it in an environment variable and effectively use it in order to simulate what the RewriteBase directive does.
However, in the .htaccess file you've posted you are only making use of RewriteBase in the very last rule (on the very last line). So, you only need to target that one rule (no need for an environment variable).
However, there are other ways to effectively calculate the RewriteBase (or rather calculate the root URL-path and store this in an env var), depending on what you are trying to do. So, you may not need to do this at all.
There are other errors/inconsistencies in the file you posted that also make me question whether this is really what you should be doing.
Anyway, focusing on the last rule (which is the only place in your file that RewriteBase applies)....
RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php?/$1 [L]
To read from the external file file_name.txt (in the document root) and use the contents of this file as the base URL-path in the substitution string in the last RewriteRule directive then you could do something like this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond expr "file('%{DOCUMENT_ROOT}/file_name.txt') =~ m#^(/.*)#"
RewriteRule (.*) %1index.php?/$1 [L]
The %1 backreference (to the capturing group in the preceding CondPattern) holds the contents of the first line of the text file (since the dot does not match newlines). Although, this regex is rather generic (it matches everything after an initial slash - to the end of the line) - you may want to make this more restrictive, to ensure that it only matches a valid URL-path you are expecting.
If file_name.txt does not exist (or cannot be read) then a non-fatal rewrite:error occurs (check your error log) and the rule simply fails.
And file_name.txt contains a single line of the form:
/path/to/dir/
CodePudding user response:
Thanks, guys. I found a solution. I stored my script in a variable and dynamically create and update the htaccess file each time I need to. Here's my working code
$htaccess = fopen("./.htaccess", "w") or die("Unable to open file!");
fwrite($htaccess, $ht_input);
fclose($htaccess);
