7

I have a .htaccess that maps a domain to a folder.

RewriteEngine On
RewriteBase /

# redirect mapped domain
ReWriteCond %{HTTP_HOST} joshblease.uk.to
ReWriteCond %{REQUEST_URI} !gme-index/
ReWriteRule ^(.*)$ gme-index/$1 [L]

Is there any way to edit/add extra domain maps to the file using PHP?

Simply, I want to get the contents of the .htaccess file and add to them using a PHP script.

MD XF
  • 7,860
  • 7
  • 40
  • 71
Blease
  • 1,380
  • 4
  • 38
  • 64
  • Nooooooooooooooo never edit .htaccess with PHP, you're just asking for security issues... – Cyclone Jan 17 '12 at 18:35
  • 6
    @Cyclone: If it's done properly it's not a security risk. – ThiefMaster Jan 17 '12 at 18:36
  • Nothing yet and i've looked around for quite a while, sorry if its a lot. I'm a novice developer. Its like cpanel and how http redirects are added with that. – Blease Jan 17 '12 at 18:36
  • 1
    Just to throw this out there: presuming that your PHP script is a web script, letting a web script update the web server configuration (which is what .htaccess is) is a potential security disaster. Sometimes it might not be avoidable, but tread with great care. And yes, what have you tried? – Conrad Shultz Jan 17 '12 at 18:36
  • 2
    If you are not experienced with that, you should rather set up a `RewriteMap` which is easier to append to without syntax woes. – mario Jan 17 '12 at 18:37
  • 2
    @ConradShultz: I don't think you can compare those. Editing the *real* config files would require root privileges while .htaccess files are usually owned by a user accessible by the PHP scripts anyway. So a malicious/bugged script could edit them anyway. – ThiefMaster Jan 17 '12 at 18:39
  • @ThiefMaster: Sure, if it's not based on any user input whatsoever, or if it's filtered securely, and your code is sound...but otherwise you may find yourself opening a rather large security hole. – Cyclone Jan 17 '12 at 18:39
  • If its easier, I want to point domains to my ip and map them to different folders but with a web "control panel" – Blease Jan 17 '12 at 18:40
  • Question is why do you want to do that? .htaccess file should be not be edited many times a day. – anubhava Jan 17 '12 at 18:42
  • I want different domains mapped to different folders and I can't add addon domains as my hosting only allows a few. If not .htaccess, then is there another way of doing this? – Blease Jan 17 '12 at 18:45
  • 2
    @ThiefMaster: The extent to which .htaccess can screw things up is determined in large part by other variables (e.g. AllowOverride). We don't know how that's set up in this case. Further, my .htaccess files are explicitly non-writable by Apache so they *can't* be edited by a malicious script for that reason. – Conrad Shultz Jan 17 '12 at 18:47

3 Answers3

10

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.

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • Is there anyway to do this without the httpd.conf file? I'm on shared hosting. – Blease Jan 17 '12 at 22:54
  • Unfortunately no because `RewriteMap` directive only goes into httpd.conf file. However it will be just **one time** change in httpd.conf. – anubhava Jan 18 '12 at 04:48
4

This could work for your situation:

Ammend the .htaccess to look the following:

RewriteEngine On
RewriteBase /

# redirect mapped domain
ReWriteCond %{HTTP_HOST} joshblease.uk.to
ReWriteCond %{REQUEST_URI} !gme-index/
ReWriteRule ^(.*)$ gme-index/$1 [L]

###CUSTOM RULES###

php script assuming $rules holds the new generated rules to be ammended;

$htaccess = file_get_contents('/path/to/.htaccess');
$htaccess = str_replace('###CUSTOM RULES###', $rules."\n###CUSTOM RULES###", $htaccess);
file_put_contents('/path/to/.htaccess', $htaccess);

example above is theory and has not been tested, would be dependant upon .htaccess privledges and permissions of the script.

Jason Brumwell
  • 3,482
  • 24
  • 16
0

You could use the following htaccess to map any domain other than maindomain.com to a folder that has the same name as the domain-name.

RewriteCond %{ENV:REDIRECT_STATUS} ^$
ReWriteCond %{HTTP_HOST} !maindomain.com
ReWriteCond %{HTTP_HOST} (.*)
ReWriteRule ^(.*)$ %1/$1 [L]

alternatively; to map the folder to the domainname without the ltd.

RewriteCond %{ENV:REDIRECT_STATUS} ^$
ReWriteCond %{HTTP_HOST} !maindomain.com
ReWriteCond %{HTTP_HOST} ^([^\.]*)
ReWriteRule ^(.*)$ %1/$1 [L]

Not exactly what you wanted, but it might work for you, and doesn't need any php.

Gerben
  • 16,747
  • 6
  • 37
  • 56