Apache rewrite to different places - apache

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?

Related

Redirect all URLs to a single page in the httpd.conf

I am looking for a way to redirect an entire website to one single domain. I want to do this in the httpd.conf file. For example the VirtualHost I have looks like:
<VirtualHost xx.xx.xx.xx>
ServerName website.com
ServerAdmin webmaster#website.com
ScriptAlias /cgi-bin/ /usr/local/apache2/htdocs/cgi-bin/
DocumentRoot /usr/local/apache2/htdocs/website
CustomLog logs/website/access_log combined
ErrorLog logs/website/error_log
Redirect / http://newwebiste.com
Redirect /xxx http://newwebiste.com
</VirtualHost>
Currently the redirects above still keep the full URL.
So if I want to http://webiste.com/test/thispage.html I would redirect to http://newwebsite.com/test/thispage.html. I just want it to go to newwebsite.com and lose the rest.
So..
http://webiste.com/test/thispage.html
to
http://newwebsite.com
What do I need to add to the httpd.conf to achieve this?
Thanks,
As you have noticed Redirect catches all and sends the same URI to the target which is what you don't want, so you will use RedirectMatch, to match all possible requests, but not adding anything to the target url.
So you can just use:
RedirectMatch ^ http://newsite.example.com/
As a side note about mod_rewrite:
I certainly do not recommend mod_rewrite as recommended by others unless you really have a task at hand which requires the complexity only mod_rewrite can offer, and this is not the case.
Side note 2:
For examples it is better to use "example.com" domain name since it is not a valid domain and it is permanently reserved for example purposes.
Remove
Redirect / http://newwebiste.com
Redirect /xxx http://newwebiste.com
and add
RewriteRule ^ http://newwebsite.com [L,R=301]

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.

How to redirect directory to another domain with Apache?

I have an Apache server with two domains (via VirtualHost) on it.
The file setup is like this: /example1/example2/
example1 is the root for www.example1.com
example2 is the root for www.example2.com
I would like for www.example1.com/example2/<whatever else> to redirect to www.example2.com/<whatever else>
I have access to the main Apache configuration file. Any tips on how to do this? I'm not very familiar with configuring Apache, so any explanations would be very much appreciated.
The other questions related to this one lacked explanations, so I felt I had to post my own question.
You can use an .htaccess file to do this pretty quickly. In www.example1.com/example2/, you would create this .htaccess file.
<VirtualHost *:80>
...
<Directory /path/to/vhost/>
RewriteEngine on
RewriteBase /example2/
RewriteRule ^(.*)$ http://www.example2.com/$1 [R=301]
</Directory>
</VirtualHost>
This does assume that you want the redirect to be a 301, and that the example2 folder has AllowOverride All set.
This rule works using regex to capture the incoming URL, sans the example2 bit, then appending it to the rewritten URL. The R in the square brackets tells Apache to use a Location header to the rewritten URL. The =301 tells Apache to use a 301 Permanent Redirect header.
If tou have mod_alias active, you can use the Redirect directive:
<VirtualHost *:80>
ServerName example1.com
Redirect 301 /example2 http://www.example2.com
</VirtualHost>

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

How do I redirect a user using Apache Rewrite, to the fully qualified domain name?

I'm really new to apache mod_rewrite module. I have a page called http://abc in my company intranet. I want users to be redirected to http://abc.somecompanyname.com whenever they type http://abc to the URL bar. Could someone please provide and example or point me in the right direction.
I figure this should be quite an easy question to answer. Thanks everyone for you inputs.
-Mark
Quote from Apache 2.4 documentation:
The very best way to solve this doesn't involve mod_rewrite at all, but rather uses the Redirect directive placed in a virtual host for the non-canonical hostname(s).
<VirtualHost *:80>
ServerName undesired.example.com
ServerAlias example.com notthis.example.com
Redirect / http://www.example.com/
</VirtualHost>
<VirtualHost *:80>
ServerName www.example.com
</VirtualHost>
This does require another virtual host, but there's no shortage of those. The solution works very well for me - and I like how redirection of 'unwanted' hosts and configuration of the canonical host are separated.
You could accomplish that with a VirtualHost definition as simple as this, on the server handling requests for abc:
<VirtualHost *:80>
ServerName abc
RewriteEngine on
RewriteRule ^/(.*)$ http://abc.somecompanyname.com/$1 [R,L]
</VirtualHost>
I found the advise in the Apache2 URL Rewriting Guide worked better.
I ended up with:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^foo\.bar\.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/(.*) http://foo.bar.com/$1 [L,R]
The "RewriteEngine on" line wasn't included in the Apache2 example. Maybe it's usually on by default but in my case I needed to add it.