Can't find apache config redirect in config files - apache

the goal
Redirecting mydomain.com/something to mysite.com/something
the problem
Any domain that you throw at the server gets 302 redirected to www.mysite.com and, for the life of me, I can't find the config rule that is doing this!
what i've done so far
I've grepped everything in the /etc/apache2 folder for 'mysite.com', 'Redirect', 'redirect', and other, as well as scour by hand all the apache config files looking for this redirect rule to no avail.
Any and all help in finding the culprit redirect is greatly appreciated. I'm not a sysadmin so there must be a place I don't know of where this might be other than the /etc/apache2 folder...
Thank you kindly for helping me with my first SO question :)

The file which includes redirection rules does not have to be in /etc/apache2 folder. Check your document root folder (the path of which you can find in your vhost configuration file) for .htaccess files. They may have redirect rules too.

The problem was with an .htaccess in the docroot of another domain on the server. This other domain was higher alphabetically and was treated as the default domain.
I solved the problem by creating a new vhost for the domain that i wanted to redirect and it was handled properly.
Thank you all for your help!

Related

Subdomain is redirecting to primary domain files

I have created a subdomain(development.domain.com.np). Everytime when i hit the subdomain url it shows subdomain link but uses primary domain index.php file.
But when remove index.php file from my subdomain and replace it with index.html it works fine.
I am not able to figure out what is causing this.
I have checked redirects and logs but everything seems fine.
Do I have to add anything in htaccess file?. Any help is highly appreciated. Thank you.
You need to create 2 virtualhosts in your apache configuration pointing to 2 different documentRoots: one for each virtualhost. This way each virtualhost will handle just one domain: the documentRoot in each virtulahost will show only the files related to that specific domain.

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.

Does the recursive .htaccess rule affects subdomains pointing to subdirectories?

Suppose I have a site example.com which points to the root directory of my hosting account and it has a .htaccess file in that directory. Say I have an other domain stuff.example.com which points to the same hosting account, but to a subdirectory of it, not to the root directory, and that subdirectory also has a .htaccess file in it.
If I visit stuff.example.com then will its .htaccess file be affected by the .htaccess settings of the root directory? Or htaccess search starts from the directory where the domain points to, so in this case the htaccess file in the parent directory is not taken into account?
Surprisingly the Apache docs don't ever explicitly answer this, as far as I can tell. But the htaccess tutorial gives the example that if a file is requested from /www/htdocs/example, then Apache looks for the following .htaccess files:
/.htaccess
/www/.htaccess
/www/htdocs/.htaccess
/www/htdocs/example/.htaccess
which presumably leads outside of the DocumentRoot. So it seems that the answer to your question is yes, Apache will read all .htaccess files all the way up to /.
will its .htaccess file be affected by the .htaccess settings of the root directory?
Yes. Where your web root is doesn't matter.

How does htaccess recursion work?

I'm working in a hosted situation where I have a primary webroot located in ~/www. I have several subdomains hosted in ~/www/__subdomains. In the primary webroot, I have a .htaccess file that does a bit of minor rewriting. In two of my subdomains, I have similar .htaccess files and these subdomains respond properly.
In 2 other, newly created subdomains, I have .htaccess files that are empty save for an AddHandler directive. What I'm finding is that the root .htaccess file's existence/content affects these 2 subdomains. They throw a 500 error. As soon as I rename that one, the broken subdomains work.
This, of course, breaks the other sites so I can't just move this off, but it violates my understanding of how .htaccess recursion works. I thought that as long as there was a .htaccess file in a subdirectory, those in ancestor directories would never get executed.
Clearly, I'm wrong about that so I'm hoping someone can educate me and help me get this fixed.
Thanks.
.htaccess files are applied from the current directory up, stopping at the main configuration. Any rules and directives that are in the current directory's .htaccess file, supersede any other rules found while evaluating .htaccess further up.
However, the problem, as you've found, is that rules that aren't explicitly overridden, are applied from the other files. You can reference the Apache .htaccess Tutorial for further explanation, specifically, the How directives are applied section.
Hope that helps.

How can I redirect requests to specific files above the site root?

I'm starting up a new web-site, and I'm having difficulties enforcing my desired file/folder organization:
For argument's sake, let's say that my website will be hosted at:
http://mywebsite.com/
I'd like (have set up) Apache's Virtual Host to map http://mywebsite.com/ to the /fileserver/mywebsite_com/www folder.
The problem arises when I've decided that I'd like to put a few files (favicon.ico and robots.txt) into a folder that is ABOVE the /www that Apache is mounting the http://mywebsite.com/ into
robots.txt+favicon.ico go into => /fileserver/files/mywebsite_com/stuff
So, when people go to http://mywebsite.com/robots.txt, Apache would be serving them the file from /fileserver/mywebsite_com/stuff/robots.txt
I've tried to setup a redirection via mod_rewrite, but alas:
RewriteRule ^(robots\.txt|favicon\.ico)$ ../stuff/$1 [L]
did me no good, because basically I was telling apache to serve something that is above it's mounted root.
Is it somehow possible to achieve the desired functionality by setting up Apache's (2.2.9) Virtual Hosts differently, or defining a RewriteMap of some kind that would rewrite the URLs in question not into other URLs, but into system file paths instead?
If not, what would be the preffered course of action for the desired organization (if any)?
I know that I can access the before mentioned files via PHP and then stream them - say with readfile(..), but I'd like to have Apache do as much work as necessary - it's bound to be faster than doing I/O through PHP.
Thanks a lot, this has deprived me of hours of constructive work already. Not to mention poor Apache getting restarted every few minutes. Think of the poor Apache :)
It seems you are set to using a RewriteRule. However, I suggest you use an Alias:
Alias /robots.txt /fileserver/files/mywebsite_com/stuff/robots.txt
Additionally, you will have to tell Apache about the restrictions on that file. If you have more than one file treated this way, do it for the complete directory:
<Directory /fileserver/files/mywebsite_com/stuff>
Order allow,deny
Allow from all
</Directory>
Can you use symlinks?
ln -s /fileserver/files/mywebsite_com/stuff/robots.txt /fileserver/files/mywebsite_com/stuff/favicon.ico /fileserver/mywebsite_com/www/
(ln is like cp, but creates symlinks instead of copies with -s.)