Redirect subdomain to subdirectory - apache

i've already read all topics here and on google about redirection but i'm a programmer and i cant get it going.
I have Apache 2.2 installed. The web root is C:\Apache\htdocs. My network admin set me up a local domain that points to the server with Apache. The domain is myPhpApp.ourcompany.local. And this subdomain works, it shows the Apache "It works" page.
Now i have a website in C:\Apache\htdocs\myPhpApp and i want Apache to redirect the myPhpApp.ourcompany.local to this directory. The URLs should stay while browsing the website always as myPhpApp.ourcompany.local for example: myPhpApp.ourcompany.local/index.php, myPhpApp.ourcompany.local/data.php and so on.
I dont know how to achieve this? Mod-rewrite, virtual hosts, combination of both?
i have got this and this does not work:
<VirtualHost myphpapp.ourcompany.local>
DocumentRoot /myphpapp/
ServerName www.example1.com
RewriteEngine on
RewriteRule ^/$ /myphpapp/ [R]
</VirtualHost>
In effect i get:
Forbidden
You don't have permission to access
/myphpapp/ on this server.
Can anyone help?
EDIT
Maybe i forgot to mention: i dont put this into the www root which is C:/Apache/htdocs becuase i have more apps in there.
i have 3 directories in thdocs: myphpapp, myoldapp, mytestapp. As a target i want to have 3 subdomains that point to each directory.

I think it will be a combination of Nikola's and cromestant's answers:
<VirtualHost *:80>
DocumentRoot "C:/Apache/htdocs/myPhpApp"
ServerName myPhpApp.ourcompany.local
</VirtualHost>

First of all, you don't need a rewrite rule for simple thing as this.
I assume you need following VirtualHost definition
<VirtualHost *>
DocumentRoot /myphpapp/
ServerName myphpapp.ourcompany.local
</VirtualHost>
"VirtualHost *" part tells Apache on which interface and optionally port to listen to.
ServerName tells which domain name will be used to identify this virtual host.

Document root in your apache config should point to your directory where you have the app, in what you stated in your question it should be
DocumentRoot C:\Apache\htdocs\myPhpApp
and that is all.
restart or reload your apache, and test.

Related

Apache VirtualHosts multiple ServerAliases with different TLDs

I am working with several domains which all follow a similar pattern of redirects. Instead of writing out each domain as it's own virtual host, I am attempting to make the file more maintainable using only one:
<VirtualHost *:80 *:443>
ServerName domain.xz
ServerAlias *.domain.xx *.domain.xy
RewriteEngine on
RewriteRule ^/(.*)$ https://xz.newdomain.com/$1 [QSA,NC,L,R=301]
</VirtualHost>
In the above example domain.xx will redirect to xz.newdomain.com correctly however domain.xy will not. I have checked the documentation and cannot determine the reason that this does not work.
Is it possible to make this work as I intend or will I have to make them separate VirtualHost configurations?
You should add also second level domains names in ServerAlias, i.e:
ServerAlias domain.xx *.domain.xx domain.xy *.domain.xy

Rewrite spare domains to main domains with .htaccess

We have a site with an English and Spanish version, each on a different domain. We also have a few spare domains for each language which we'd like to redirect to the language's main domain.
Specifically:
estadiosfutbol.net/..., estadiosfutbol.org/... and estadiosfutbol.info/... should all redirect to https://estadiosfutbol.com/...
worldfootballstadiums.com/..., worldfootballstadiums.info/..., worldfootballstadiums.org/... and worldfootballstadiums.net/... should all redirect to https://worldstadiums.football/...
I'm struggling with the rewrite rules so any help would be greatly appreciated.
There are two ways this can be done. The first is the simpliest, but is not always practical.
First Method
This method does not require HTACCESS files. In your Apache server configuration you just need to add ServerAliases for each of the domains that you want it to handle. (You must make sure all the domains are pointing at the same machine)
The Code
NameVirtualHost *:443
<VirtualHost *:443>
ServerName estadiosfutbol.com
ServerAlias estadiosfutbol.info estadiosfutbol.net estadiosfutbol.org
DocumentRoot /www/domain
</VirtualHost>
<VirtualHost *:443>
ServerName worldstadiums.football
ServerAlias worldfootballstadiums.com worldfootballstadiums.net worldfootballstadiums.info worldfootballstadiums.org
DocumentRoot /www/otherdomain
</VirtualHost>
Note: This will only redirect if the user tries to access the website using SSL. (eg ) If you want it to redirect all traffic from both port 80 and port 443 you would need to make separate virtual hosts and use the second method to achieve the redirection.
Second Method
The second way is a little more complicated, but works in almost all situations. There a two main steps that need to be carried out in order for this to work properly:
Make sure that whatever server software you are using is setup to be looking for all the domains. The server has to have a VirtualHost(Apache) that is listening for each domain in order for the next step to do anything.
Create a .HTACCESS file under each domains' root that looks similar to this:
The Code
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !estadiosfutbol.net$ [NC]
RewriteRule ^(.*)$ https://estadiosfutbol.com/$1 [L,R=301]
Note: You will need to change the third line on each domain to be the domain to rewrite from (eg estadiosfutbol.net/, estadiosfutbol.org/ and estadiosfutbol.info)
Note: Changing the forth line is all that is required for the separate domain.

Redirect but keep the domain the same

I have an owncloud server, and I would like to setup a second short domain, to keep the shared links short.
lets say we've the longdomain.com and short.com
Heres is my httpd lines
<VirtualHost *:80>
ServerAdmin email#adderss.com
DocumentRoot /var/www/dir/public_html
ServerName short.com
ServerAlias www.short.com
RewriteEngine on
RewriteRule ^/([A-Za-z0-9]{4,12})$ https://www.long.domain.com/public.php?service=shorty_relay&id=$1 [QSA,L]
ErrorLog /var/www/dir/error.log
</VirtualHost>
With the current lines, short.com redirects to exactly where I need, but I would like this redirect to be on the background and keep the short domain on the user's browser.
How can I do this?
Update:
with this in my short domain virtual host I can visit my owncloud using the short domain.
For example: short.com/index.php/apps/files/
the long domain is vanished. I think Im one step forward now.
ProxyPass / https://www.long.domain.com
ProxyPassReverse / https://www.long.domain.com
The next step is to use the the regex so I can load only shorty id links.
How can I combine the rewrite regex above with the proxypass.
I've tried ProxyPassMatch butI havent figured out how to use it properly
Any ideas?
If you can't make the same content addressable by short.com, short.com can proxy to the long domain by loading mod_proxy, mod_proxy_http, and changing your rewrite flag from R to P.

Can I configure Apache on my laptop to forward to real site for all requests except one?

I want to run some local tests on a site I have. The site is accessible at www.mysite.com. I want one particular file to be fetched from my local machine. I thought I could maybe achieve this by
installing Apache locally
adding 'localhost www.mysite.com' to my hosts file
configure Apache to forward all requests to www.mysite.com except for requests for the particular file www.mysite.com/myapp/myfile.css, which should be served from the Apache web server running locally.
Firstly I am not sure whether that set-up would work - in the case where a file is requested that is not my special case, the request would be forwarded to www.mysite.com/... , but would that then (because of the entry in my hosts file) go back to my local Apache server and into some infinite loop?
Secondly (and only relevant if the above is not true), how would I configure Apache to do that? I guess I need a ProxyPass but I'm having trouble figuring out exactly what.
Thanks for any help.
Paul
I don't think you'll be able to do this the way you're suggesting as you'll never be able to perform a lookup to proxy to www.mysite.com if you've defined it as localhost.
You could create another domain in your hosts file, say local.mysite.com and host the desired website files there and proxy everything else to www.mysite.com:
<VirtualHost *:80>
ServerName local.mysite.com
DocumentRoot ...
<Directory ...>
...
</Directory>
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/myapp/myfile.css
RewriteRule ^(.*)$ http://www.mysite.com/$1 [P]
</VirtualHost>
Or if www.mysite.com works directly using the IP (i.e. not via virtual hosting) you could point localhost to mysite.com and use the real IP in the rewrite proxy.
<VirtualHost *:80>
ServerName www.mysite.com
DocumentRoot ...
<Directory ...>
...
</Directory>
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/myapp/myfile.css
RewriteRule ^(.*)$ http://1.2.3.4/$1 [P]
</VirtualHost>

Apache Redirects in HTTPS

I have an Apache server (https://mysite.com/sub/en/). It's setup with SSL on port 443, and in the VirtualHost tags:
<VirtualHost _default_:443>
Redirect permanent /sub https://mysite.com/sub/en/
...
</VirtualHost>
Basically, when the user comes to the site, I want them to be forced to the "/en" sub-directory. However, if you try to visit https://mysite.com/sub, it doesn't redirect you to the sub "/en" directory.
I'm really not sure what is going on. I do have a similar setup in my non-ssl virtualhost tags:
<VirtualHost *:80>
Redirect permanent / https://mysite.com/sub/en/
Redirect permanent /sub https://mysite.com/sub/en
</VirtualHost>
These rules work fine. So if the user attempts http://mysite.com/sub, it forwards them to the SSL version, and the "/en" subdirectory like I want it to. But if you try the SSL version: https://mysite.com/sub, it fails to forward.
Do I have the configurations wrong? Am I not using the right redirect or rewrite rule?
To get this working the way I wanted, I ended up using a RewriteRule:
RewriteRule ^/sub$ https://mysite.com/sub/en/
This appears to have done the trick.