Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

Assuming the following folders:

/
  index.html

/folder1/
  index.html
  page.html

(So, root with index.html and folder1 with index.html and page.html.)

I want links in page.html pointing to "/" to link to /folder1/index.html.

Also, in PHP, for example, if I include('/file.php');, it should take it from /folder1/ and not root.

Basically, any file inside of folder1 to consider / to be /folder1/.

I tried RewriteBase /folder1/ in .htaccess but that didn't work.

How can this be done, please?

share|improve this question
    
You have two separate problems there. That links should be rebased to the subfolder, which you should be able to "fix" with mod_rewrite by looking at the referrer header and modifying the base. The other are the php includes, which I don't think you can "fix" without changing the PHP code. I could be wrong about the PHP part, which is why I'm posting as a comment. –  Chris S May 23 '12 at 13:42
    
I was hoping there was a way to make a folder be considered as root with htaccess. Like shared-hosting companies do for example. It's localhost I need it for so I can change anything. –  Francisc May 23 '12 at 14:44

1 Answer 1

up vote 1 down vote accepted
+50

Here is the problem with what you're trying to do:

  • A client requests /folder1/index.html, which includes a link to /file.php.
  • The client makes a request for /file.php. At this point, the server has no idea that this request originated from a file located inside of /folder1, so there is no rewriting magic that can make this work.

Here are a few potential solutions:

  1. You can set up multiple <VirtualHost> blocks in your configuration, each with a different DocumentRoot. This requires that you either control DNS or that you control the hosts file for all clients accessing your server.

    For example, you would do something like this in your server configuration:

    <VirtualHost *:80>
      ServerName server1
      DocumentRoot /var/www/server1/html
    </VirtualHost>
    
    <VirtualHost *:80>
      ServerName server2
      DocumentRoot /var/www/server2/html
    </VirtualHost>
    

    Each virtual host points to a different folder, so if you request http://server1/, all links will resolve to documents inside /var/www/server1/html, and similarly for server2. As I mentioned, this presumes that you can either make server1 and server2 show in DNS for your clients, or that you can add something like the following to the client host files:

    your.server.ip server1 server2
    

    Since you've said, "it's localhost and I can change anything", this seems like the easiest and most effective solution.

  2. You could look into the use of an Apache output filter to rewrite all the links in your documents. That is, if someone requested a /folder1/index.php, Apache would rewrite links that look like /file.php to /folder1.php. Generally for this sort of thing I would recommend mod_proxy_html, but I'm not entirely sure if this will operate outside of a proxy setting.

    If mod_proxy_html won't work, you could try using mod_sed. You would probably have to build this separately for anything earlier than Apache 2.4, and it will be much trickier to get right.

    I recommend the VirtualHost based solution.

share|improve this answer
    
Thank you, larsks. Excellent. –  Francisc May 27 '12 at 11:10

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.