How can I use httpd.conf to force www - apache

I am trying to force the www. in the beginning of my domain...
I am aware of using the method mentioned on the first answer here to do this in the httpd.conf file, but this causes Apache to say that it doesn't understand this command. Is there something I am doing wrong... or do I need to put the code in a certain spot?
-Thanks, San

Related

Need to configure .htaccess, so multiple folders will act as if they are their own separate root folders - for the code running on them

For example:
mydomain.com/site1
mydomain.com/site2
I need to install an application on /site1 that will think that it is on the root folder. (In this case PHP, js, CodeIgniter, but could be anything)
So for example, links/references for files such as "/file.jpg" (in code that is in the site1 folder, such as at mydomain.com/site1/code.js) will really load from mydomain.com/site1/file.jpg
And also the code would not be able to see any folder below site1, so that is basically the root folder. And similar thing would be at site2, so the 2 are separate root folders.
I thought this would be some kind of simple .htaccess file installed at mydomain.com/site1 with a redirect, or some kind of a reverse proxy, but so far everything I tried did not work.
I can't seem to find even any such example even on stack overflow..
Any ideas?
The easiest way to do this would be to create an additional VirtualHost, for internal use, called internal1, whose RootDirectory is, you guessed it, /var/www/mydomain.com/htdocs/site1 where the main site is in /var/www/mydomain.com/htdocs.
Then in mydomain.com you reverse proxy /site1 to internal1 (you'll have to put it into /etc/hosts and alias for localhost). The second request will have its DOCUMENT_ROOT point to site1, as requested (and its ServerName changed to internal1):
ProxyPass /site1/ http://internal1/
ProxyPassReverse /site1/ http://internal1/
(Not sure about the trailing slashes)
Now, accessing yourdomain.com/site1/joe.html will trigger a second internal connection to internal1/joe.html, which will contain, say, 'src="/joe.jpg"'; and here's where ProxyPassReverse will come into play, rewriting this in 'src="yourdomain.com/site1/joe.jpg"' so that everything will work.
errata corrige
The above is not correct, thanks #MrWhite for pointing this out. ProxyPassReverse is not enough as it only rewrites headers. From the Apache documentation (emphasis mine):
Only the HTTP response headers specifically mentioned above will be
rewritten. Apache httpd will not rewrite other response headers, nor
will it by default rewrite URL references inside HTML pages. This
means that if the proxied content contains absolute URL references,
they will bypass the proxy. To rewrite HTML content to match the
proxy, you must load and enable mod_proxy_html.
(The method is dirty as all Hell: every HTTP call incurs one extra connection and two rewrites, one going in, a larger one going out).
Of course, if the link is built using e.g. Javascript, it might well be that the proxy code will not recognize it as a link, will leave it unchanged, maybe with the "internal1" name inside somewhere, and the app will break.
However, #arkascha has the right of it - you should cure the cause, not the symptom. You can maybe rewrite the environment of the apps so that they run without troubles even if they are in a subdirectory. Or you could try injecting <base href="https://example.com/site1"> in the output HTML.

Subdomains not working on Apache

I would like to have a subdomain (webmail.inkieweb.com) for my website which points to the same folder as www.inkieweb.com/webmail (/var/www/webmail/), but whenever I access the subdomain, I'm instantly redirected to inkieweb.com (/var/www/) instead. This is bothering me as some of my clients are requesting subdomains and I have no idea why they're not working.
EDIT: I was previously working with a CNAME. I removed that and added an "A" record instead. Now it's not redirecting. It is still loading /var/www/ instead of /var/www/webmail/ though.
These are what I think are the relevant files on my server: http://pastebin.com/Gf6ZSjPv
Note that even though the last two files have the full hostname in the <VirtualHost inkieweb.com:80> section, I have also tried having everything with *:80 instead. Still nothing.
The only thing that happens sometimes is that when I'm messing around with a2ensite and a2dissite, instead of loading the home page, http://www.inkieweb.com starts loading the webmail instead.
Please help me. I don't know what else I can do.
Do you get any errors when you start up apache? Things like inkieweb.com has no virtualhosts?
Are there any virtualhosts defined in your httpd.conf file that may be overriding the one your working with?
I would stick with the *:80 and make sure your have serveraliases for each one that make sense and that the server aliases are not overlapping. You can use grep to find all of them.

Unwanted Apache redirect behavior

My Apache installation on my Ubuntu 12.04 server redirects requests addressed to:
http://87.73.120.126
to
http://90.184.18.96/joomla/index.php/jomsocial
The address 87.73.120.126 is my servers current IP-address. The address 90.184.18.96 was the one it had half a year ago.
My question is: why does it do this? and how do I make it stop?
Half a year ago I wanted to make requests to http://90.184.18.96 redirect to http://90.184.18.96/joomla/index.php/jomsocial
I did this by adding this line:
RedirectMatch permanent ^/$ http://90.184.18.96/joomla/index.php/jomsocial
to the file /etc/apache2/apache2.conf
A couple of days ago I tried to solve the current redirect problem by changing the line above to:
RedirectMatch permanent ^/$ http://127.0.0.1/joomla/index.php/jomsocial
and rebooting the server. This did not solve the problem!
Then I tried to do a global file search for the string 90.184.18.96 using this command:
sudo grep 90.184.18.96 / -irn --exclude-dir={bin,lib64,opt,sbin,tmp,boot,lost+found,proc,selinux,dev,media,root,srv,lib,mnt,run,sys,mail,log,doc,src,recovery-mode} --color
this search revealed a couple of outdated CRON-entries that I also fixed to use 127.0.0.1 instead. This didn't help either. So, where could the string '90.184.18.96' be residing? I guess it must be somewhere in order for anything to redirect to that address.
This server runs Joomla from the path /var/www/joomla/. As this path is not excluded by my grep-search any redirection emerging from Joomla should be revealed by the grep-search.
Two considerations:
Redirecting to 127.0.0.1 will redirect to the user's machine, not your server. If you're only spanning one server, you should not include your server's IP (it's not nice to access a site with a domain i.e. www.your-domain.com and be redirected to an ip address). So the rule should use variables for the host: %{HTTP_HOST} and %{REQUEST_URI} for host and path.
Besides httpd.conf you may also have redirect rules in your .htaccess which resides in the webserver root: and it's a hidden file so it could have been skipped by your grep, have a look into that as well. Beware the syntax of .htaccess and httpd.conf is slightly different.
You're achieving two things with that redirect: point to an installation which is off-root (/joomla) and setting a different homepage. Setting the homepage in Joomla is as easy as marking its menu item with a star in the menu item editor, so you might not need the second part.

How do I configure apache for a custom directory?

Trying to configure apache2 to load example.com/forum/ from a different document root, relative to the site root. Forums are installed somewhere else on the server.
Is there a directory alias command? I've found the alias configuration entry for apache, but had no luck.
Basically, I want example.com to have the same directory its always had, but example.com/forum/ to be hosted somewhere else, on the same server.
I tagged this question with mod_rewrite because I thought maybe it would be the key, here.
Cheers!
Alias is the right way, unless you have some subtlety that you didn't reveal in your question.
# http.conf
Alias /forum /usr/lib/bbs/ # or whatever
The job of Alias is to take the abstract URL coming into your system and map it to a concrete filesystem path. Once it has done that, the request is no longer an URL but a path. If there is no Alias or similar directive handling that URL, then it will get mapped to a conrete path via DocumentRoot.
If this isn't working, you have to debug it further. Are you getting errors when you access /forum? Look in the error log.
It all depends of what you want. You can "hardlink" with real path and it works (so you were right to think it could work with mod_rewrite).
Quick sample (that works on my production domains) to make an internal change (I add a subdirectory):
RewriteRule (.*) %{DOCUMENT_ROOT}/mysubfolder%{REQUEST_FILENAME} [QSA,L]
So you can easily do something like:
RewriteRule ^/forum/(.*) %{DOCUMENT_ROOT}/mysubfolder%{REQUEST_FILENAME} [QSA,L]
And my suggestion would be that if you plan to have more rewrite rules, keep everything homogeneous, i.e.: keep on using only rewrite rules, so use my suggestion above. This way you'll not get a bad mix of Alias, RewriteRules and so on. For nice and clean stuff: keep everything homogeneous.

Where is the proper place for mod_rewrite entries?

For the love of God, I can't seem to get this mod_rewrite working properly. Instead of doing brute force trial-and-error, let me ask here.
I want mod_rewrite rules to apply to ALL domains.
I want mod_rewrite entries in httpd.conf
I want to get rid of this WWW virus (for SEO purposes):
http://www.example.com > http://example.com
I want to get rid of index.html (for SEO, google indexes it instead of just domain):
http://www.example.com/index.html > http://example.com
http://www.example.com/some/index.html > http://example.com/some/index.html
Domains are inside <virtualhost> entries. I couldnt figure out where to put what or which one should take priority. As i mentioned, I would like to apply these 2 rules to ALL DOMAINS in the server.
The situation is exacerbated by ssl.conf. Will all these need to be entered into ssl.conf too? What will happen when there are 2 redirects like:
http://www.example.com/index.html > http://example.com/index.html > http://example.com
Thank you so much. This has quickly become all so confusing.
Maria
This solves it for me. As I suspected, there is a whole lotta difference where rewriterule is applied. Many people including mean seems to be unaware of this.
http://wiki.apache.org/httpd/RewriteContext
The Apache HTTPD Server deals with requests in discrete phases. While this is usually transparent to the user and administrator it does have an effect on the behaviour of mod_rewrite when rulesets are placed in different contexts. To oversimplify a little, when rules are placed in VirtualHost blocks (or in the main server context) they get evaluated before the server has yet mapped the requested URI to a filesystem path. Conversely, when rules are placed in .htaccess files, or in Directory blocks in the main server config, they are evaluated after this phase has occured.