0

I'm trying to symlink my public folder with a index.php file in it to the httpdocs folder where the public folder is also placed.

When I try and Symlink the public folder I get an error alerting me that the public folder name is already taken. I've also tried Symlinking the index.php file itself which worked but broke the functionality of the site.

Here is the command I'm using when Symlinking the public folder.

ln -s /var/www/vhosts/mysite.co.uk/httpdocs/public /var/www/vhosts/mysite.co.uk/httpdocs

Here is the command I used when Symlinking the index.php file.

ln -s /var/www/vhosts/mysite.co.uk/httpdocs/public/index.php /var/www/vhosts/mysite.co.uk/httpdocs

tldr: I want to use the index.php in the public folder as the index.php file when a user visits my site without having to use mysite.co.uk/public/index.php. The index.php file has to remain in the public folder.

Cœur
  • 37,241
  • 25
  • 195
  • 267
PapaSmurf
  • 1,035
  • 2
  • 14
  • 26

3 Answers3

1

If I understand correctly, you want to access index.php from httpdocs/public, and that folder already exists (as the error in the first case informs). You can link the file to that (exisitng) folder:

ln -s /var/www/vhosts/mysite.co.uk/httpdocs/index.php /var/www/vhosts/mysite.co.uk/httpdocs/public/index.php

Note that you need to specify the file itlsef that you want to link

Attila
  • 28,265
  • 3
  • 46
  • 55
  • Yes the file I'm wanting to use as the "homepage" is already in the public folder `httpdocs/public/index.php`. To access this now I would have to visit `mysite.co.uk/public/index.php` but I want to just visit `mysite.co.uk` and get the same result, will this solution work for me? – PapaSmurf Apr 17 '12 at 15:26
  • If I was to put the source in a different place I'd have the same problem, it would jus create a `/public` folder/symlink in my `httpdocs` – PapaSmurf Apr 17 '12 at 15:29
  • It needs to be within the /mysite.co.uk/ folder. – PapaSmurf Apr 17 '12 at 15:32
  • See last update (I hope I got it right this time :)). You need to specify the file name in both cases and the first path is the [one created](http://unixhelp.ed.ac.uk/CGI/man-cgi?ln) – Attila Apr 17 '12 at 15:33
  • Then use: `ln -s /var/www/vhosts/mysite.co.uk/index.php ...` – Attila Apr 17 '12 at 15:33
  • Ypu can do both `ln -s /var/www/vhosts/mysite.co.uk/index.php ...` and `ln -s /var/www/vhosts/mysite.co.uk/httpdocs/index.php ...` – Attila Apr 17 '12 at 15:42
  • I've managed to set it up but now I'm getting this error on my webpage `Warning: Unknown: open_basedir restriction in effect. File(/home/vhosts/mysite.co.uk/app/public/index.php) is not within the allowed path(s): (/var/www/vhosts/mysite.co.uk/httpdocs:/tmp) in Unknown on line 0` – PapaSmurf Apr 17 '12 at 15:49
  • Is (/home/vhosts/ the "real" path to the file? It seems the symbolic link is resolved before the file is accessed: 1) you need to add the proper directory to the allowed paths or 2) use hard link instead of the symbolic link (do not set `-s` for `ln`) – Attila Apr 17 '12 at 15:53
  • What is the difference between hard link and no -s? – PapaSmurf Apr 17 '12 at 15:55
  • Soft link is a "reference" to the real file: the file system needs to follow the reference to get to the real file. Hard links are the [same file](http://justlikeamagic.com/2010/04/06/hard-links-vs-soft-links/): think of physically copying the file, but if you make changes to one, the other will change as well. – Attila Apr 17 '12 at 16:01
1

I've managed to get it to work! I used a soft symlink on public/index.php in the httpdocs folder to make a index.php file also in the httpdocs folder.

ln -s /var/www/vhosts/mysite.co.uk/httpdocs/public/index.php /var/www/vhosts/mysite.co.uk/httpdocs/

I then added a .htaccess file to the httpdocs folder with the following...

Options +FollowSymLinks
<IfModule mod_rewrite.c>
        RewriteEngine on

        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d

        RewriteRule . index.php [L]
</IfModule>

Thank you to @Attila for sticking with me and helping out!

PapaSmurf
  • 1,035
  • 2
  • 14
  • 26
-1

Solution (if all files are required in …/public and …/httpdocs.

  • move your data from /var/www/vhosts/mysite.co.uk/httpdocs/public to /var/www/vhosts/mysite.co.uk/httpdocs
  • symlink /var/www/vhosts/mysite.co.uk/httpdocs to …/public

Command: ln -s /var/www/vhosts/mysite.co.uk/httpdocs /var/www/vhosts/mysite.co.uk/httpdocs/public

Cons:

  • infinite recursion, for instance /var/www/vhosts/mysite.co.uk/httpdocs/public/public/public/public/

It is not possible to symlink to an existing file/directory. If it would be possible, you would break your tree structure. In fact you are trying to link a directory to its parent. How should your system resolve overwritten paths?

Example:

You have a file /a/b/c and it would be possible to do the following:

ln -s /a/b /a

How should the system resolve the now not reachable file /a/b/c?

Move your data into the desired directory instead of trying to break your file tree.

It is possible to mount data to existing directories (original data will be unreachable until unmount), but this would not help in your case.

Rayne
  • 2,620
  • 20
  • 28
  • Thanks for your answer, If you read the comments in the other answer I tried this and got an error... – PapaSmurf Apr 17 '12 at 15:56
  • It works but you just did not explain what you are doing. For instance /home is not /var and if you do not correctly set up your PHP configuration (open_basedir *hint*), we are not able to help you. – Rayne Apr 17 '12 at 17:23