redirect download directory to subdomain - apache

my download script structure is:
http://www.mysite.com/download/viewdownload/**/**
now i want move and redirect to subdomain
for example
redirect
http://www.mysite.com/download/viewdownload/61/4249
to
http://www.alt.mysite.com/download/viewdownload/61/4249
or
http://www.mysite.com/download/viewdownload/53/824
to
http://www.alt.mysite.com/download/viewdownload/53/824

You can use either mod_alias:
Redirect /download/viewdownload http://www.alt.mysite.com/download/viewdownload
In the vhost config or htaccess in the document root of the www.mysite.com host, or use mod_rewrite:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?mysite\.com$ [NC]
RewriteRule ^/?download/viewdownload/(.*)$ http://www.alt.mysite.com/download/viewdownload/$1 [L,R]

Related

Redirect documentroot to another folder in conf apache or .htccess

Good Day
I want to rewrite a url when request a subdirectory
When the user request
172.0.0.1/url
i want to rewrite to make this url point to
172.0.0.1/url/url
or the documentroot is in /var/www/html/url/url make this point to
172.0.0.1/url
The trick here is to avoid creating a rewrite and a redirect loop. You can put this code in your root folder htaccess
RewriteEngine On
# Redirect /url/url/xxx to /url/xxx
RewriteCond %{THE_REQUEST} \s/(url)/url/(.*)\s [NC]
RewriteRule ^ /%1/%2 [R=301,L]
# Internally rewrite back /url/xxx to /url/url/xxx
RewriteRule ^(url)/((?!url/).*)$ /$1/$1/$2 [L]
You can use .htaccess to redirect /url to /url/url
Just Create .htaccess file at /var/www/html/ with following contains:
RewriteEngine on
RewriteRule ^/?url/(.*)$ /url/url/$1 [R,L]
This will Redirect all incoming requests at /url and redirect them to /url/url
But you cannot redirect requests coming at /url/url to /url Because we are already redirecting request at /url, this will result in catch 22 and browser will keep redirecting back and forth between /url and /url/url.

htaccess redirect 301 (both www and non-www)

I have two domains:
http://www.domain.com
and
http://domain.com
each pointing to the same directory. Now I got a new domain (http://foo.com)
Unfortunately a catch all directive would not work, because names changed for subdirectories. I need about 20 single redirects, e.g.
/file1.html -> /newdir/
/x.html -> /y/
...
That means, both http://www.domain.com/dir1 and http://domain.com/dir1 should be redirected permanently to http://foo.com/newdir1. And the same with the other directories. How can I achive that via htaccess redirects?
Place this rule in root .htaccess of domain.com:
RewriteEngine on
ewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule ^ http://foo.com%{REQUEST_URI} [L,R=301]
Try this
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^folder1(.*)$ http://www.newdomain.com/$1 [L,R=301]

rewrite rule, two domains pointing to same directory, redirect one file at one domain

How to possible redirect a single file having two domains pointing to the same directory.
I have:
domain.com
domain2.com
going to /var/www
if I hit domain2.com I go to /var/www/index.php
I'd like to have a rewrite rule that states domain2.com/index.php should go to domain2.com/thefile.php
Thanks
Try adding this to the htaccess file in /var/www:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?domain2\.com$ [NC]
RewriteRule ^(index\.php)?$ /thefile.php [L]
If you want to redirect the browser, you'll need to add a R to the rule's flags: [L,R]

Redirect wildcard subdomains to subdirectory, without changing URL in address bar

I've read a lot of questions and answers about this on here but none that seem to solve my specific problem.
I want to redirect any subdomain to the subdirectory to match.
So: x.domain.com would go to domain.com/x, and y.domain.com would go to domain.com/y - But I want to do this without the URL in the address bar changing.
Here's what I have so far:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} !^(www)\. [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com [NC]
RewriteRule ^ /%1 [P,L]
But this takes me to a website redirect loop, with an incorrect address in the URL bar where the subdomain still exists.
For example, x.domain.com takes me to x.domain.com/x and I get a redirect loop error.
I'd be grateful if anyone can point me in the right direction! Nothing I change seems to work...
First of all, make sure that the vhost in the apache configuration is properly configured and all subdomains of domain.com are in the same host configuration (wildcard):
<VirtualHost *:80>
ServerName domain.com
ServerAlias *.domain.com
...
You can get the redirect working with the following htaccess configuration:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com
RewriteRule ^(.*)$ http://domain.com/%1/$1 [L,NC,QSA]
Now, if you open asd.domain.com it should redirect you to domain.com/asd.
You will still have the problem, that the redirect is visible in the URL address bar. In order to prevent this, enable mod_proxy (and load the submodules) on your server and exchange the "L" flag with the "P" flag:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com
RewriteRule ^(.*)$ http://domain.com/%1/$1 [P,NC,QSA]
If this doesn't work, viewing the vhost configuration and the content of error.log on subdomain calling will be helpful!
References:
.htaccess rewrite subdomain to directory
http://httpd.apache.org/docs/2.2/mod/mod_proxy.html
http://httpd.apache.org/docs/2.2/rewrite/flags.html#flag_p
This can be achieved in .htaccess without mod_proxy provided your server is configured to allow wildcard subdomains. (I achieved that in JustHost by creating a subomain manually named *). Add this to your .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.website\.com$
RewriteCond %{HTTP_HOST} ^(\w+)\.website\.com$
RewriteCond %{REQUEST_URI}:%1 !^/([^/]+)/([^:]*):\1
RewriteRule ^(.*)$ /%1/$1 [QSA]
I named the subdirectories under $_SERVER['DOCUMENT_ROOT'] match with my subdomains like so:
/
var/
www/
html/
.htaccess
subdomain1.domain.com/
subdomain2.domain.com/
subdomain3.domain.com/
Where /var/www/html stand as 'DOCUMENT_ROOT'. Then put following code in the .htaccess file:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/%{HTTP_HOST}/
RewriteRule (.*) /%{HTTP_HOST}/$1 [L]
It works as redirect wildcard subdomains to subdirectories, without changing URL in address bar.
Beside of vhost, you may also put the subdirectories outside root and access it using alias as described here. Then put the same .htaccess code in that location.

Redirect traffic to CDN page

So anytime someone visits this page:
http://www.site.com/page/image_(.*).png
They get redirected to the cdn url, which would be
http://cdn.site.com/page/image_(.*).png
How can I do that using htaccess? I only need this for that url.
Using mod_alias, if you have access to vhost config, in your main vhost (for www) add:
RedirectMatch 301 ^/page/image_(.*)\.png http://cdn.site.com/page/image_$1.png
If you don't have access and have to use an .htaccess file, you need mod_rewrite:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?site.com [NC]
RewriteRule ^page/image_(.*).png http://cdn.site.com/page/image_$1.png [R=301,L]