3

I want to set a php_value flag only for a specific url (rewrited) path. I'm using htaccess to accomplish this. The framework I'm using is CodeIgniter, so there is one htaccess file and url routes are handled by php.

Only the backend of the website should have php_value max_input_vars 3000. The url is http://www.example.com/admin/dashboard

I was thinking about this in htaccess file:

<Location /website.com/admin>
    php_value max_input_vars 20000
</Location>
machineaddict
  • 3,216
  • 8
  • 37
  • 61

3 Answers3

4

Okay since you cannot upgrade your Apache to latest versions here is one work-around way to get this conditional setting in place.

1 - In the DOCUMENT_ROOT Run this command to create a symbolic link of index.php as __anything.php

ln -s index.php __anything.php

2 - Put the new code in DOCUMENT_ROOT/.htaccess just below RewriteEngine On line. You overall .htaccess would look like this:

# forward all URLs starting with /admin/ to __anything.php
RewriteRule ^(admin(?:/.*|))$ __anything.php?/$1 [L,NC]

# set max_input_vars to 2000 for file __anything.php
<FilesMatch "^__anything\.php">
    php_value max_input_vars 20000
</FilesMatch>

# your regular Codeignitor controller code
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]

PS: I don't have CI installed so I cannot really test this with CI but otherwise I tested this and got enhanced max_input_vars value.

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • 1
    Your idea gave me another idea. I created an `admin.php` file next to `index.php` file, which requires `index.php` (I was not sure if I could create symlinks). And I made a small modification to htaccess as well, like [this](http://pastebin.com/XrUC1Maj). Thank you very much! – machineaddict Sep 16 '13 at 08:06
  • THat is perfect, if you are on unix/linux then creating symlink is also a possibility. But I am really glad that it all worked out for you. – anubhava Sep 16 '13 at 08:12
  • Hi, what is the preferred way if I have Apache 2.4.18 installed? Thanks. – sidon Jul 23 '19 at 13:16
  • This should work for Apache 2.4 as well. You just need PHP 5.3.9 and later. – anubhava Jul 23 '19 at 13:43
1

You could use the following, where admin is your controller in Codeigniter:

<FilesMatch "^(admin)">
    php_value max_input_vars 20000
</FilesMatch>

If you want to target multiple CI controllers you can separate them with a pipe:

<FilesMatch "^(admin|another)">
    php_value max_input_vars 20000
</FilesMatch>

This probably isn't the best way to do what you are looking for, but it works. I would recommend using <Location> or <LocationMatch> which would needs to be set in your server config or virtual host (since you cannot use <Location> or <LocationMatch> in .htaccess).

An example of that would be:

<Location /admin>
    php_value max_input_vars 20000
<Location>
doitlikejustin
  • 6,293
  • 2
  • 40
  • 68
  • 3
    Doesn't [FilesMatch](http://www.askapache.com/htaccess/using-filesmatch-and-files-in-htaccess.html) requires a file which actually exists? – machineaddict Sep 10 '13 at 05:46
  • I still cannot make it work. The admin url is rewritten like this `http://192.168.1.10/example.com/admin/statistics/costs_view`. I have tried with `/example.com/admin/`, `/admin/`, `/index.php/admin/` and other cominations, they don't work. I print the `max_input_vars` like this `var_dump(ini_get('max_input_vars'))` in both admin and frontend. – machineaddict Sep 11 '13 at 07:32
1

What if trying to set the value hardcoded in php code while init the admin controller:

<?php
ini_set('max_input_vars', 2000);

Should be work if the server config allows to change the ini at runtime.

sensi
  • 569
  • 4
  • 15