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 http://www.oldurl.com and now its like http://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
Redirectdirective won’t be bound to theRewriteCondconditions and will always redirect /my-page to http://www.newurl.com/your-page, also, mod_rewrite has precedence over mod_alias so theRewriteRule (.*)$ http://www.newurl.com/$1 [R=301,L]rule gets applied before theRedirectdirective gets looked at. However, if the .htaccess file sits within the document root of both the oldurl.com and newurl.com domains, theRedirectdirective will be applied after the browser gets redirected to http://www.newurl.com/my-page, thus redirecting (again) to http://www.newurl.com/your-pageSo, 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: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:
Where the /path/to/file/map.txt will look something like:
And your combined rules would look like: