Redirecting old domian to new domain by .htaccess - apache

I am in process of migrating my old domain to new domain using Apache Mod-Rewrite moduled and its .htaccess file.
we have almost same structure of the new domain which includes
URL's
Database
except the domain name, like it was www.oldurl.com and now its like www.newurl.com and this is what i have in my .htaccess file of Old domain
RewriteEngine On
RewriteCond %{HTTP_HOST} ^oldurl.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.oldurl.com$
RewriteRule (.*)$ http://www.newurl.com/$1 [R=301,L]
Above settings seems to be working fine except in one case, we have few URL's in my old domain which has either been removed or structure has been changed so in that case above rule will not work.i came to know about adding something like this in my .htaccess file beside what i have described above
RewriteEngine On
RewriteCond %{HTTP_HOST} ^oldurl.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.oldurl.com$
RewriteRule (.*)$ http://www.newurl.com/$1 [R=301,L]
Redirect 301 /my-page http://www.newurl.com/your-page
i have total of 20+ such URL's and i am wondering do i need to map those all 20+ URL to there new URL's and will i need to take care about any order in which they should be put in the file.
i am also wondering how Apache will work, will it look at each mapped URL for any match? or it works in some other way?

The Redirect directive won't be bound to the RewriteCond conditions and will always redirect /my-page to http://www.newurl.com/your-page, also, mod_rewrite has precedence over mod_alias so the RewriteRule (.*)$ http://www.newurl.com/$1 [R=301,L] rule gets applied before the Redirect directive gets looked at. However, if the .htaccess file sits within the document root of both the oldurl.com and newurl.com domains, the Redirect directive will be applied after the browser gets redirected to http://www.newurl.com/my-page, thus redirecting (again) to http://www.newurl.com/your-page
So, it doesn't matter what order you have these in, since mod_rewrite gets applied first. If you have 20 URLs that need to redirect to new ones on your new site, you can enumerate them each in their own Redirect. Otherwise, if you'd rather not have the browser get redirected twice, you can enumerate them using the mod_rewrite engine:
RewriteEngine On
# redirect the changed URLs individually
RewriteCond %{HTTP_HOST} ^oldurl.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.oldurl.com$
RewriteRule ^my-page$ http://www.newurl.com/your-page [R=301,L]
RewriteCond %{HTTP_HOST} ^oldurl.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.oldurl.com$
RewriteRule ^my-page2$ http://www.newurl.com/your-page2 [R=301,L]
RewriteCond %{HTTP_HOST} ^oldurl.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.oldurl.com$
RewriteRule ^my-page3$ http://www.newurl.com/your-page3 [R=301,L]
# Finally, redirect everything else as-is
RewriteCond %{HTTP_HOST} ^oldurl.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.oldurl.com$
RewriteRule (.*)$ http://www.newurl.com/$1 [R=301,L]
Note that order does matter here. Having to repeat the 2 Conditions for HTTP_HOST is kind of ugly, you can maybe get around that by using the SKIP, but it's probably better to just repeat them. But if you have access to your server config or vhost config, take a look at the RewriteMap Directive, which allows you to create a mapping of, in your case, old urls to new urls and you can reduce all the individual changed url rewrites to a single one:
Inside your server/vhost config, something like this:
RewriteMap newurls txt:/path/to/file/map.txt
Where the /path/to/file/map.txt will look something like:
my-page your-page
my-page2 your-page2
my-page3 your-page3
etc...
And your combined rules would look like:
RewriteCond %{HTTP_HOST} ^oldurl.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.oldurl.com$
RewriteRule ^(.*)$ http://www.newurl.com/${newurls:$1} [R=301,L]
RewriteCond %{HTTP_HOST} ^oldurl.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.oldurl.com$
RewriteRule (.*)$ http://www.newurl.com/$1 [R=301,L]

Related

RewriteCond for a folder only on a specific domain extension

I have a site that can be accessed via 3 domains (domain.com, domain.ch, domain.fr). The three use exactly the same files and folder, though.
Regarding the .fr domain (and only this domain), I need the following:
redirect domain.fr (root) to domain.com/fr/france.
This has been achieved with the following rule:
RewriteCond %{HTTP_HOST} ^www\.domain\.fr$
RewriteRule ^/?$ "http\:\/\/www\.domain\.com\/fr\/france" [R=301,L]
It works. (There's also a functioning rule to force www. in front of every URL.)
What I can't get to work is:
redirect domain.fr/fr also to domain.com/fr/france.
and finally, redirect any URL domain.fr/fr/* to domain.com/fr/*
(keeping whatever * stands for).
The trick (to me) is that the same .htaccess file will also be present on domain.com and domain.ch, but those rules must not activate for those domains.
You can put these rules in your htaccess
# Redirect [www.]domain.fr and [www.]domain.fr/fr to www.domain.com/fr/france
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.fr$ [NC]
RewriteRule ^(|fr)$ http://www.domain.com/fr/france [R=301,L]
# Redirect domain.fr/fr/* to domain.com/fr/*
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.fr$ [NC]
RewriteRule ^fr/(.*)$ http://www.domain.com/fr/$1 [R=301,L]
Try :
RewriteCond %{HTTP_HOST} ^www\.domain\.fr$
RewriteCond %{REQUEST_URI} !^/fr/france
RewriteRule ^fr/(.*)$ http://www.domain.com/fr/france/$1 [R=301,L]

Redirection through .htaccess not working

I have a domain which has sub-domains (add-on). I am trying to redirect users from main (old) domain to one of sub-domains (new).
First I tried to redirect everything that user types within the OLD domain except one page and one directory... Now this EXCEPT feature is going out of control. I have been trying and implementing all possible options but some it gives an error and stops redirection.
Here are the lines from .htaccess file which I am trying:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} oldrootdomain.com [NC]
RewriteRule ^/view_card.php(.*)$ http://newsubdomain.com/view_card.php$1
RewriteRule ^/m/(.*)$ http://oldrootdomain.com/m/$1
RewriteRule ^(.*)$ http://newsubdomain.com/ [R=301,L]
It seems you run into an inifinite loop.
You DON'T want to redirect for those exception. So try some additional RewriteConds
RewriteCond %{HTTP_HOST} oldrootdomain.com [NC]
RewriteCond %{REQUEST_URI} !view_card.php$
RewriteCond %{REQUEST_URI} !m/.*$
RewriteCond %{REQUEST_URI} !m$
RewriteRule ^(.*)$ http://newsubdomain.com/ [R=301,L]
Also you seem to have not understood the RewriteCond/RewriteRule thing entirely
Any successive stream of RewriteConds does only affect the directly following RewriteRule

Trying to redirect blog.domain.com to www.domain.com/blog/

I'm combining a main site at www.domain.com, and an old wordpress blog at blog.domain.com, into one completely new Wordpress install. I have exported and imported all the old blog posts so that they now live under wwww.domain.com/blog//
I am trying to create one rewrite rule that will map all the old blog posts to their new URLS.
I've tried variations on these SO discussions:
DNS/.htaccess files to redirect subdomain to specific folder
Apache rewrite rule different if capture is empty
but nothing is working.
The following in my .htaccess file will redirect blog.domain.com to www.domain.com/blog, if there is nothing more in the URL:
RewriteCond %{HTTP_HOST} ^blog\.domain\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.blog\.domain\.com$ [NC]
RewriteRule ^/?$ http://www.domain.com/blog/$1 [R=301,L]
but if there is anything more in the URL, it does not rewrite the URL at all, and goes to the new Wordpress site's 404 page.
I tried adding a capture to the final RewriteRule line, but then no rewrite occurs, and it just goes to the new homepage but the address bar still reads "blog.domain.com":
RewriteCond %{HTTP_HOST} ^blog\.domain\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.blog\.domain\.com$ [NC]
RewriteRule ^/(.+)$ http://www.domain.com/blog/$1 [R=301,L]
Is there a way to do what I'm trying to do?
You want to remove the leading slash:
RewriteCond %{HTTP_HOST} ^blog\.domain\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.blog\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/blog/$1 [R=301,L]
# no slash---^
And make the + a *. URI's that are sent through rules in htaccess files have the leading slash stripped off, so ^/(.+)$ would never match.
Replace your rule to this:
RewriteCond %{HTTP_HOST} ^(www\.)?blog\.domain\.com$ [NC]
RewriteRule ^ http://www.domain.com/blog%{REQUEST_URI} [R=301,L]
PS: Make sure this is the 1st rule in your .htaccess.

Using .htaccess to redirect by domain, either in the URL or in HTTP_HOST

I am hosting several domains on the same apache server and I want to set them all up so that /home is unique to each domain. (suppose I have 3 domains example.com example1.com and example2.com) I have setup my file structure like this...
/www/htdocs/domain/example
/www/htdocs/domain/example1
/www/htdocs/domain/example2
http://example.com/home/ ==> .../domain/example/
http://example1.com/home/ ==> .../domain/example1/
http://example2.com/home/ ==> .../domain/example2/
So I have an htaccess rule like this
RewriteCond %{HTTP_HOST} ^(.+)\.(.+)$ [NC]
RewriteRule ^home/(.*)$ /domain/%1/$1 [L]
This works perfectly. But I want to extend it to include www.example.com So I add this rule.
RewriteCond %{HTTP_HOST} ^(.+)\.(.+)\.(.+)$ [NC]
RewriteRule ^home/(.*)$ /domain/%2/$1 [L]
That works great, and is generally how I have been building my .htaccess file. It is growing quite unweildy and I need to rethink my approach. So I am trying to concatenate the above two rules into a single block. Here is what I have but it isn't working...
RewriteCond ^home/
RewriteCond %{HTTP_HOST} ^(.+)\.(.+)$ [OR]
RewriteCond %{HTTP_HOST} ^.+\.(.+)\.(.+)$ [OR]
RewriteRule ^home/(.*)$ /domain/%2/$1 [L]
Additionally and separately I would like a rule that would cause the following URL to return error 404.
http://example1.com/domain/example/logo.jpg
I am answering my own question. At least the first part. I am still looking for an answer to the second part.
RewriteCond %{REQUEST_URI} ^/home/
RewriteCond %{HTTP_HOST} ^.+\.(.+)\.(.+)$ [OR]
RewriteCond %{HTTP_HOST} ^(.+)\.(.+)$
RewriteRule ^home/(.*)$ /domain/%1/$1 [L]

How can I change all subdomains to read different content from their respective subdirectories?

I have my DNS configured to accept any subdomain (wildcard *), but I am having trouble feeding back the required content to the browsers.
I would like each subdomain to return the relative content, which resides in subdirectories of the same name within the public_html path of my server.
eg, example.domain.com/picture.jpg would actually request the file at public_html/example/picture.jpg
Currently I have tested the followed .htaccess code, but it is not functional:
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$ [NC]
RewriteRule ^(.*)$ %1/$1 [L]
This code, and similar tests, can redirect based on the subdomain (%1) fine, but the request string ($1) seems to be the issue.
Maybe you could take a look at the mod_vhost_alias module :
http://httpd.apache.org/docs/2.0/mod/mod_vhost_alias.html
Try the following:
RewriteCond %{HTTP_HOST} ^([0-9a-z-]+).domain.com [NC]
RewriteRule ^/(.*) http://domain.com/%1/$1 [L]
I didn't test this one but I use similar rules for proxying:
RewriteCond %{HTTP_HOST} !^domain.local [NC]
RewriteCond %{HTTP_HOST} ^([0-9a-z-]+).domain.com [NC]
RewriteRule ^/(.*) http://domain.local/%1/$1 [P]