htaccess internal rewrite across domains? - apache

I'm trying to point a subfolder from one domain to another on my vhost (mediatemple). I want to use internal rewrites, not 301 redirects. Here's the goal
http://www.clientdomain.com/blog/$1 --> http://www.mydomain.com/wpmu/clientdomain/$1
On the server side, the structure looks like this:
/x/y/z/domains/clientdomain.com/html/blog/ -- htaccess file is here
/x/y/z/domains/mydomain.com/html/wpmu/ -- wpmu installation
So far I've only had success with 301 redirects, but my goal is to mask things such that wpmu can power the client's blog without revealing its location. Here's my working 301 redirect:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/blog/
RewriteRule ^(.*)$ http://www.mydomain.com/wpmu/clientdomain/$1 [NC]
Is there an easy way to convert it to an internal rewrite? I haven't seen anything but 301 redirects for this type of thing...
Thanks in advance,
Casey

From what I can tell, the only way to achieve this rewrite across vhosted domains is to use a symbolic link between domains to fool mod_rewrite into thinking it's doing an internal rewrite

What you are doing is an internal rewrite.
For a 301 redirect you would have to write [L,R=301]:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/blog/
RewriteRule ^(.*)$ http://www.mydomain.com/wpmu/clientdomain/$1 [L,R=301]

Related

htaccess - Redirecting all traffic to another domain except existing 301's

My htaccess file is filled with 301 redirects like such:
Redirect 301 /old-page.html https://www.example.com/new-page
There are about 100 of these redirects. What I would like to do is redirect all traffic going to the old site to go to the new site excluding the existing 301's
So if someone goes to old-site.com/old-page.html it will take them to new-site.com/new-page and if someone goes to old-site.com/random-page.html it will take them to new-site.com - just the home page.
Is it possible to do this using mod_rewrite and mod_alias without rewriting the current 301's?
You can keep all your 301 rules. Just insert this generic 301 rule below your existing rule:
# all existing 301 rules go here
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)?old-site\.com$ [NC]
RewriteRule ^ http://new-site.com/? [L,R=301]
You need to use a RewriteCond in front of all your rules like this:
RewriteCond %{HTTP_HOST} ^(?:www\.)domain\.com$ [NC]
If you want all the following rules to be processed as well DO NOT use the L (last) flag in the RewriteCond statement.
Source: Redirect all urls exactly, just change domain name

How to redirect from domain to subdomain?

My site is www.mysite.com and I need to redirect any request to us.mysite.com.
So:
www.mysite.com ----> us.mysite.com
www.mysite.com/hello.php ----> us.mysite.com/hello.php
// etc
I tryed this but doesn't work:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mysite.com$
RewriteRule (.*) http://us.mysite.com$1 [R=301]
It looks like your RewriteCond is only matching domains that start and end with mysite.com. This does not include www.mysite.com.
The following will 301 redirect anything NOT at us.mysite.com to us.mysite.com:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^us.mysite.com$
RewriteRule ^(.*)$ http://us.mysite.com/$1 [R=301]
There are several different solutions. The best one, both from SEO and User perspective, is the one-to-one 301 redirect. It preserves your link juice and at the same time redirects the client to the exact location on the new website.
If you have mod_alias enabled, I would suggest a simple
RedirectMatch 301 ^(.*)$ / http://new.domain.com/$1
The result instruction can be accomplished with
RewriteEngine On
RewriteRule (.*) http://new.domain.com/$1 [R=301,L]
The second one is the best choice if you need to chain multiple conditions and filters. For example, if you need to redirect only certain hosts or clients depending on User Agent header.
From here.

Htaccess 301 only part of the redirect works

Hi Im moving a site from one domain to another, and I have created the following .htaccess file, but its not working.
*#Options +FollowSymlinks
RewriteEngine On
redirect 301 http://www.el-netshop.dk/pi/Dækkape_UG150_12_lysegrå_5302_.aspx http://www.el-netsalg.dk/pi/Dækkape_UG150_12_lysegrå_5271_.aspx
RewriteCond %{HTTP_HOST} ^el-netshop.dk$ [OR]
RewriteCond %{HTTP_HOST} ^www.el-netshop.dk$
RewriteRule (.)$ http://www.el-netsalg.dk/$1 [R=301,L]
I would like it to work like this.
Have a list of urls where the url is diffent, with more then just the domain. Ex. in the above the from link contains 5302 but to link is 5271.
Then with the rest, I want it to make a normal redirect.
The above code just do (.*)$ http://www.el-netsalg.dk/$1 and ignores the special cases.
What am I doing wrong?
According to the apache docu the syntax is as folows:
Redirect 301 /service http://foo2.bar.com/service
So try:
Redirect 301 /pi/Dækkape_UG150_12_lysegrå_5302_.aspx http://www.el-netsalg.dk/pi/Dækkape_UG150_12_lysegrå_5271_.aspx
without the "http://www.el-netshop.dk" for the old-path paramater.

Redirect with htaccess for images onto another server without redirect looping

I currently have a host where my main site is hosted on. I have set up nginx on another server to mirror/cache files being requested if it doesn't have it already, in particular images and flv videos.
For example:
www.domain.com is my main site.
www.domain.com/video/video.flv
www.domain.com/images/1.png
I would like to ask apache to redirect it to imgserv.domain.com (imgserv.domain.com points to another server IP)
imgserv.domain.com/video/video.flv
imgserv.domain.com/images/1.png
Basically redirect everything with certain filetypes and preserving the structure of the URL, like flv etc.
I tried something but I am getting a redirect looping error. Could someone help me out?
Thank you!
This is what I have at the moment
RewriteEngine ON
RewriteCond %{HTTP_HOST} ^www.domain.com$ [NC]
RedirectMatch 302 ^(.*)\.gif$ http://imgserv.domain.com/forums$1.gif
RedirectMatch 302 ^(.*)\.jpg$ http://imgserv.domain.com/forums$1.jpg
RedirectMatch 302 ^(.*)\.png$ http://imgserv.domain.com/forums$1.png
You are mixing up two different modules: RewriteEngine and RewriteCond are from mod_rewrite while RedirectMatch is from mod_alias. They can’t work together.
Try this mod_rewrite example instead:
RewriteEngine ON
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^.*\.(gif|jpg|png)$ http://imgserv.example.com/forums/$0 [L,R]

301 redirect .htaccess

Im trying to request the following entire site 301 redirect:
word.something.blah.domain.com --> http://www.word.com
I don't know how to write the 301 redirect rule.
Can someone help out?
I will assume you are using the same directory to serve files on both domains. In which case, a Redirect clause won't work (infinite redirect loop).
With mod_rewrite, you can check the value of the current HTTP_HOST and take a decision based on that:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^([a-zA-Z0-9]+)\.something\.blah\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.%1.com/$1 [R=301,NE,L]
put this into root directory of the subdomain:
Redirect permanent / http://www.word.com
If you are keeping everything else the same - that is, the file names - but simply changing the domain, this code is all you need to put on the OLD DOMAIN htaccess:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.newdomain\.co.uk
RewriteRule (.*) http://www.newdomain.co.uk/$1 [R=301,L]