How do I set the local document root in Apache? - apache

So this is my directory structure
/
/test
index.php
blah.php
blah.php
So in /test/index.php I have a link such as this
Link
but I want it to link to /test/blah.php, not the blah.php in the root directory. Basically, I want to set a local document root. Is this possible to set this using .htaccess or in the httpd.conf?

If an a tag on /test/index.php has its href set to /blah.php, then it's the browser that's interpreting that as pointing to a file in the document root. So you can't achieve what you want without changing the way you're generating the href attribute.
You have a couple of options for this:
You can omit the forward slash to generate links relative to the current URL instead of the document root. A link in /test/index.php pointing to blah.php will be interpreted as /test/blah.php.
You can write some custom code to generate your links. You could have a function my_special_link ($link) that takes in blah.php and prepends the current file's directory, for example.

Add this to your httpd.conf or better yet put it in a virtualhost directory
NameVirtualHost *:80
<VirtualHost *:80>
ServerName blah.localhost
DocumentRoot C:\web\test
</VirtualHost>
In WINDOWS\System\system32\etc\bin or somewhere ( LOOK for 'hosts' file ), edit hosts file so it has
127.0.0.1 blah.localhost
Restart apache and go to blah.localhost in the browser.

Related

Compute Engine Custom domain accessibility

I have a Google Compute Engine instance(installed apache, php, mysql etc.) and a custom domain name. I can do add my Custom Domain to my Google Compute Engine.
Lets my domain name be www.try.com .
my instance's "www" folder;
--->try
--->somethingelse2
--->somethingelse3
..
How can I set "www.try.com" access to only "try" folder? Thanks.
In deciding what file to serve for a given request, httpd's default behavior is to take the URL-Path for the request (the part of the URL following the hostname and port) and add it to the end of the DocumentRoot specified in your configuration files. Therefore, the files and directories underneath the DocumentRoot make up the basic document tree which will be visible from the web.
If a directory is requested (i.e. a path ending with /), the file served from that directory is defined by the DirectoryIndex directive. For example, if DocumentRoot were set as above, and you were to set:
DirectoryIndex index.html index.php
You only need to modify the path in the Directory index.
You can find more information here

Add www when accessing root domain, not using htaccess file

I have an apache server with some websites built in Wordpress, using vhosts.
The thing is that I have for all of them a configuration like:
ServerName site1.com
ServerAlias www.site1.com
When I access to Site1 through "site1.com" the URL changes to "www.site1.com". The same for Site2, Site3, etc. But for SiteN it's inverse. If I access to "siten.com" it keeps the URL and if you go to "www.siten.com" it changes to "siten.com".
I know I can change this using htaccess file, but my doubt is why some sites has a default and the new site has another default? All the htaccess have the same things and the vhost configuration is the same for all.
Thank you,
Done! The change should be implemented in Wordpress configuration, is not a htaccess issue

Apache symlink to a folder outside of web root

Here is the folder layout.
/outside is not normally accessible anywhere on example.com
/public_html is the folder for http://example.com
How can I use Apache's htaccess to make the url http://example.com/outside/ to show content from the folder /outside?
Thanks!
How can I use Apache's htaccess to make the url http://example.com/outside/ to show content from the folder /outside?
You can't. As far as I have found out, Apache prevents directives in .htaccess files linking to locations outside the current web root.
You would have to do this in the central configuration:
Alias /outside /path/to/your/outside
You may be luckier with a symlink if you can turn FollowSymLinks on.

Apache root-relative links

Using apache 2, I have a simple virtual host container:
<VirtualHost 127.0.0.1>
ServerName developmentServer
DocumentRoot /var/www
</VirtualHost>
The site is accessible, but I'm having trouble using root relative links within the site. I have an 'images' folder that is located at the root level. When I am within a folder or subfolder, I want to specify an image path that is root relative. (And actually all paths will be root relative regardless of the page depth.) This currently is not working:
<img src="/images/file.jpg">
But this will:
<img src="../images/file.jpg">
I understand the difference, but I do not know what needs to be changed in Apache to allow me start all links with a "/" so they are root-relative. I'm thinking there is a configuration setting, and I shouldn't have to rely on mod_rewrite. Any suggestions?
Is the image located at /var/www/images/file.jpg and are you accessing the page containing this image over HTTP (http://developmentServer/...)? Then it must work.

Magic Apache redirecting for /~username

I have inherited a webserver already serving some websites. I am trying to migrate some of those sites to a new webserver.
One of those websites has a page called:
http://mydomain/ABCDepartment/
This URL also works:
http://mydomain/~joesmith
and the index page for joesmith actually lives in /var/www.../ABCDepartment/people/joesmith/
Now I am checking in httpd.conf and I see the following:
UseCanonicalName Off
UserDir public_html
UserDir disabled root
There are no special mod_rewrite rules for joesmith or the ~
How is this magic happening? UseCanonicalName is off, and if it wasn't UserDir public_html should look in /home/joesmith/public_html
What am I missing?
This is an Apache extension called userdir: http://httpd.apache.org/docs/1.3/mod/mod_userdir.html
It automatically rewrites requests to point to a folder called public_html within the user's home directory (the web server must have read access up the tree to this folder).