Redirect but keep the domain the same - apache

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.

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

Apache rewrite to different places

I have a site:
oldsub.oldsite.com that I now need to rewrite to newsub.newsite.com
So far I have this which seems to work for the site:
<VirtualHost *:80>
ServerName oldsub.oldsite.com
Redirect 301 / http://newsub.newsite.com
</VirtualHost>
BUT
There is one page that I need to redirect to a different location on the new site.
oldsub.oldsite.com/oldpage now needs to go to newsub.newsite.com/bla/bla/bla/newpage
How do I combine both of these things?
i.e. One specific page gets redirected to the new page on the new site and everything else just goes to the home page of the new site.
Just place an exception for the more specific URL before the more general "catch all" you already have:
<VirtualHost *:80>
ServerName oldsub.oldsite.com
RewriteEngine on
RewriteRule ^/oldpage http://newsub.newsite.com/bla/newpage [L,R=301,QSA]
Redirect 301 / http://newsub.newsite.com
</VirtualHost>
You obviously need the rewrite module inside your http server for that. But it should be available with a single click for all typical distributions.
Thanks #arkascha for the advice. Got me in the write direction. This is what worked:
<VirtualHost *:80>
ServerName oldsub.oldsite.com
RewriteEngine on
RedirectMatch 301 ^/oldpage http://newsub.newsite.com/bla/newpage
Redirect 301 / http://newsub.newsite.com
</VirtualHost>
Seems like the rewriterule and redirect don't work well together?

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 subdomain to subdirectory

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.

Apache multiple URL to one domain redirect

For the last two day, I've been spending a lot of time to solve my problem, maybe someone can help me.
Problem: I need to redirect different url's to one tomcat webbase-dir used for artifactory.
following urls should point to the tomcat/artifactory webapp:
maven-repo.example.local ; maven-repo.example.local/artifactory ; srv-example/artifactory
Where maven-repo.example.local is the dns for the server-hostname: "srv-example"
I'm accessing the tomcat app through the JK_mod module. The webapp is in the ROOT directory
This is what I've got so far:
<VirtualHost *:80>
#If URL contains "artifactory" strip down and redirect
RewriteEngine on
RewriteCond %{HTTP_HOST} ^\artifactory\$ [NC]
# (how can I remove 'artifactory' from the redirected parameters? )
RewriteRule ^(.*)$ http://maven-repo.example.local/$1 [R=301,L]
ServerName localhost
ErrorLog "logs/redirect-error_log"
</VirtualHost>
<VirtualHost *:80>
ServerName maven-repo.example.local
ErrorLog "logs/maven-repo.example.local-error.log"
CustomLog "logs/maven-repo.example.local-access.log" common
#calling tomcat webapp in ROOT
JkMount /* ajp13w
</VirtualHost>
The webapp is working with "maven-repo.example.local", but with "maven-repo.example.local/artifactory" tomcat gives a 404 - "The requested resource () is not available."
It seems that the mod_rewrite doesn't have taken any effect, even if I redirect to another page, e.g google.com
I'm testing on windows 7 with maven-repo.example.local added in the "system32/drivers/hosts" file
Thanks in advance!
Thanks a lot for your hint #PHP-Prabhu
a simple:
RedirectPermanent /artifactory /.
in the apache httpd.conf did the trick!
First, all Redirects are processed before Aliases are processed, and therefore a request that matches a Redirect or RedirectMatch will never have Aliases applied. Second, the Aliases and Redirects are processed in the order they appear in the configuration files, with the first match taking precedence
Please see this URL
http://httpd.apache.org/docs/2.1/mod/mod_alias.html