Redirect with htaccess for images onto another server without redirect looping - apache

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]

Related

Redirection between domains

I have two webs on two different domains. One is on domain.com and the other web is on domain.es.
I am shutting down the web on domain.es but first I have to redirect every request to domain.es to domain.com. This is easy and I had already done it with RewriteCond & RewriteRule, like this:
www.domain.es/ -- redirects to --> www.domain.com/
But I am having a problem now. I also need to redirect one specific page from domain.es to domain.com like this:
www.domain.es/a_page/ -- redirects to --> www.domain.com/another_page/
I am having problems when redirecting with both rules, as when apache detects a request to www.domain.es/a_page it goes to www.domain.com/ and not to the specific page I need.
I have this in my virtualhost:
RewriteCond %{HTTP_HOST} ^domain.es/a_page/
RewriteRule ^(.*)& http://www.domain.com/another_page/ [R=301,L]
RewriteCond %{HTTP_HOST} ^domain.es/
RewriteRule ^(.*)& http://www.domain.com/ [R=301,L]
I've got the [L] flag so apache would stop processing rules, but this is not working.
Any help would be appreciated.
RewriteCond %{HTTP_HOST} ^domain.es/a_page/
The line above is never going to match anything - you're asking if the host is equal to domain.es/a_page/, which is a host as well as a path. You need to test against only the hostname, and then redirect based on the path, e.g.
RewriteCond %{HTTP_HOST} ^domain\.es
RewriteRule ^a_page/ http://www.domain.com/another_page/ [R=301,L]

.htaccess 301 redirect from old domain to new domain while structure of pages and url are same

I want to redirect http://olddomain.com to http://newdomain.com for my all urls..keeping the page on new domain same.
What i mean to say is URLs such as below
http://olddomain.com/home/category/page.html
http://olddomain.com/home/mybook/page2.html
http://olddomain.com/login
should be 301 redirect to the new newdomain but same pages, like below
http://newdomain.com/home/category/page.html
http://newdomain.com/home/mybook/page2.html
http://newdomain.com/login
this is what i have in my .htaccess currently
RewriteEngine on
RewriteCond $1 !^(index\.php|img|public|robots\.txt) [NC]
RewriteRule ^(.*)$ /index.php?/$1 [L]
Please help me to do this cleanly and exlpain things in details since i am new in this.
also does someone know how much time search engines might take to move away from the references of my olddomain? i mean the old-domain urls in search queries should be replaced by new-domain urls... n old domain should go away from search engines.
Add following code at the beginning of .htaccess -
RewriteEngine On
# Redirect Entire Site to New Domain
RewriteCond %{HTTP_HOST} ^olddomain.com$ [OR]
RewriteCond %{HTTP_HOST} ^another.olddomain.com$ [NC]
RewriteRule ^(.*)$ http://newdomain.com/$1 [R=301,L]
Much simpler:
Redirect 301 / http://newdomain.com/
Replace your .htaccess file with that one line OR if you have access to it, put it in the apache conf file(s) for your old domain (I place it following the DocumentRoot directive).
See Redirecting and Remapping with mod_rewrite for more info.
I usually add the following codes in .htaccess in the old website
#Options +FollowSymLinks
RewriteEngine on
RewriteRule ^(.*)$ http://newdomain.com/$1 [R=301,L]
This above code will redirect all links to new domain, you don't have to do anything, each link and images redirect to new domain link.
Beside this we have to tell Google when your site moves
If you've moved your site to a new domain, you can use the Change of address tool to tell Google about your new URL. We'll update our index to reflect your new URL. Changes will stay in effect for 180 days, by which time we'll have crawled and indexed the pages at your new URL.
Here is the link for this https://support.google.com/webmasters/answer/83106?hl=en
I have done this for 2 to 3 sites without losing seo as well. It does work.
Thanks
Tried to do
LoadModule rewrite_module modules/mod_rewrite.so
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^localhost$ [NC]
RewriteRule ^(.*)$ http://newdomain.com/$1 [R=301,L]
</IfModule>
But did not worked the way it should, was just trying to point my localhost to newdomain.com.
But when I hit localhost still it does not point.

add alias in front of each URL mod_rewrite apache

If any likes come in the format
http://localhost/index.php/bla...
I want to convert it to http://localhost/er/index.php/bla...
I'm trying the following but it seems to be looping the url indefinitely
RewriteRule ^localhost/index/php/(.*)$ localhost/er/index.php/$1 [R=301,L]
RedirectMatch 301 ^/localhost/index.php/(.*)$ localhost/er/index.php/$1
You've got 2 different things going on, a RewriteRule (mod_rewrite) and a RedirectMatch (mod_alias). You'll only need one, but neither of those can match against the hostname (localhost). If this has to only be limited to the "localhost" host, then you need to do this:
RewriteEngine On
RewriteCond %{HTTP_HOST} localhost$ [NC]
RewriteRule ^/?index\.php/(.*)$ /er/index.php/$1 [R=301,L]
Otherwise, you can just stick with mod_alias:
Redirect 301 /index.php/ /er/index.php/
Everything after /index.php/ will automatically get appended.

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.

htaccess internal rewrite across domains?

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]