Edit .Htaccess with PHP

Edit .htaccess with PHP

As suggested in one of the comments above it is better to use RewriteMap for your case here rather than trying to edit .htaccess from PHP code directly. Here is a sample how to use it:

  1. Add following line to your httpd.conf file:

    RewriteMap domainMap txt://path/to/domain-dir.txt
  2. Create a text file as /path/to/domain-dir.txt like this:

    sub1 /subdir1
    sub2 /foodir2
    foo /bar
  3. Add these line in your .htaccess file:

    Options +FollowSymLinks -MultiViews
    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$ [NC]
    RewriteRule ^$ ${domainMap:%1} [L,R]

Effectively all this means is to have these redirects in place:

  • sub1.domain.com/ => sub1.domain.com/subdir1
  • sub2.domain.com/ => sub2.domain.com/foodir2
  • foo.domain.com/ => foo.domain.com/bar

Advantage: With this setup in place, you can edit or recreate the file /path/to/domain-dir.txt as much as you want from your php code without opening a huge security hole be allowing php code o edit .htaccess directly.

edit htaccess via php

I would recommend storing IP addresses you do not want to allow in a database. This makes administration and if you want stats, they can be added easily as well (ie. How many times has this user tried to access the site after being denied, etd). You could even send certain IPs to different location if you wanted.

  • Store the IP addresses in a database table
  • When a user comes to the page check to see if their IP is in the table
  • If so increment stats counter (if desired) and redirect user to new location
  • Or continue loading the current page

Use php's Header function Header('Location: [url here]'); to redirect them and remember it must be before any other content is sent to the page.

how to change php version in htaccess in server

To switch to PHP 4.4:

AddHandler application/x-httpd-php4 .php

To switch to PHP 5.0:

AddHandler application/x-httpd-php5 .php

To switch to PHP 5.1:

AddHandler application/x-httpd-php51 .php

To switch to PHP 5.2:

AddHandler application/x-httpd-php52 .php

To switch to PHP 5.3:

AddHandler application/x-httpd-php53 .php

To switch to PHP 5.4:

AddHandler application/x-httpd-php54 .php

To switch to PHP 5.5:

AddHandler application/x-httpd-php55 .php

To switch to PHP 5.6:

AddHandler application/x-httpd-php56 .php

To switch to PHP 7:

AddHandler application/x-httpd-php7 .php

To switch to PHP 7.1:

AddHandler application/x-httpd-php71 .php

Edit .htaccess with Wordpress Plugin API?

The function in wordpress to update the .htaccess file is insert_with_markers it takes three parameters.

insert_with_markers ( string $filename, string $marker, array|string $insertion )

in following this tutorial you could write something like this

// Get path to main .htaccess for WordPress
$htaccess = get_home_path().".htaccess";

$lines = array();
$lines[] = "RewriteBase /foobar";

insert_with_markers($htaccess, "MyPlugin", $lines);

That would look like this in the your .htaccess file

# BEGIN MyPlugin
RewriteBase /foobar
# END MyPlugin

Here is a link to wordpress' documentation of that function

https://developer.wordpress.org/reference/functions/insert_with_markers/

Is it safe to modify a .htaccess file in PHP?

If the file is writable by the PHP process it can be modified like every other file.

However, instead of commenting/uncommenting your code you should use a RewriteCond checking your env var.

.htaccess file issue / can't edit .htaccess

It's your wordpress install. Anything INSIDE the #Begin wordress and #End Worpress tags get overwritten by wordpress. You need to remove ALL non wordpress rules outside of those begin and end comments.

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
#Maintenance Mode
#RewriteCond %{REMOTE_ADDR} !^111\.111\.111\.11
#RewriteCond %{REQUEST_URI} !^/berightback\.html$
#RewriteRule ^(.*)$ http://my-domain.com/berightback.html [R=307,L]"

RewriteCond %{HTTP_USER_AGENT} libwww-perl.*
RewriteRule .* ? [F,L]
RewriteCond %{HTTP_HOST} ^xxx\.xxx\.xxx\.xx
RewriteCond %{HTTP_HOST} ^my-domain\.com
RewriteRule (.*) http://www.my-domain.com/$1 [R=301,L]

# BEGIN WordPress
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress

</IfModule>

ExpiresActive on
ExpiresByType image/jpg "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/png "access plus 1 month

Specifically when you make changes to permalink.
You can see others with same issues. https://wordpress.org/support/topic/htaccess-file-being-overwritten

Why isn't .htaccess file being edited?

php_value file_uploads = On
php_value post_max_size = 100000M
php_value upload_max_filesize = 100000M
<Directory />
AllowOverride All
</Directory>

The <Directory> container is not valid inside .htaccess files and nor is it required here. The .htaccess file itself is effectively the <Directory> container.

Also, the syntax for setting the PHP config vars is incorrect, there should be no = here. For example:

php_flag file_uploads On
php_value post_max_size 100000M
php_value upload_max_filesize 100000M

Since file_uploads is a boolean, you should really use php_flag, instead of php_value to set this.

Also, whether this works at all is dependent on how PHP is installed. This is valid only if PHP is installed as an Apache module (ie. mod_php).



Related Topics



Leave a reply



Submit