Redirecting old site / subfolders to new site / subfolders - apache

I am attempting to write some redirects and have stumbled on my final step of redirecting folders.
I have an old site with the following structure,
example.com
example.com/example
example.com/example/example
example.com/example/example
(with the domain examples.com parked and redirecting to this site)
And a new site (the domain unparked),
examples.com
examples.com/examples/new
examples.com/examples/examples/updated
examples.com/examples/examples/sample
Using a traditional 301 on the original site as per the below simply redirects to root
redirect 301 /example/ http:www.examples.com/examples/new/
it will not point to the correct subfolder
As a short term I have used the below to redirect all links to the root (on the old server) but it would be ideal to push them all to their exact locations
RedirectMatch 301 ^/example/.*$ http://www.examples.com/
I'm obviously missing something simple so any suggestions are appreciated.

Try this:
RedirectMatch 301 /example(.*) http://examples.com/examples/new/$1
RedirectMatch 301 /example/example(.*) http://examples.com/examples/examples/$1
Example #1: http://oldsite.com/example/XXXX should redirect to http://newsite.com/examples/new/XXXX
Example #2: http://oldsite.com/example/example/UPDATE should redirect to http://newsite.com/examples/examples/UPDATE
ok if those above don't work..try this..
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} oldsite\.com [NC]
RewriteRule ^example/?$ http://newsite.com/examples/new [R=301,L]
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} oldsite\.com [NC]
RewriteRule ^example/example/([^/]+)/?$ http://newsite.com/examples/examples/$1 [R=301,L]
</IfModule>

Related

How to Blanket Redirect all URL's to Different Domain Sub-directory no Mask

.htaccess Rules
Redirect 301 "/page-1/" "https://www.newdomain.com/page-1/"
Redirect 301 "/anotherpage/run/" "https://www.newdomain.com/anotherpage/run/"
Redirect 301 "/" "https://www.newdomain.com/subdirectory/"
Current Results
The first two rules which are specific pages work well. The problem is with the catch all which will carry on the URI at the destination url. Only needing for anything other than specified URI rules to 301 to the new domain sub directory.
eg.
https://olddomain.com/page-5/ to https://www.newdomain.com/subdirectory/
Expected Results
https://olddomain.com/page-1/ redirects to https://www.newdomain.com/page-1/
https://olddomain.com/anotherpage/run/ redirects to https://www.newdomain.com/anotherpage/run/
https://olddomain.com/anypage/fun/ redirects to https://www.newdomain.com/subdirectory/
I've tried a couple Rewrite rules with no luck. It carries the complete URI to the new domain.
RewriteRule ^(.*)$ https://www.newdomain.com/subdirectory/$1 [R=301,L]
Check this rules on the top of your olddomain.com .htaccess file
<IfModule mod_rewrite.c>
RewriteEngine On
# custom rewrites
RewriteRule ^page-1/$ https://www.newdomain.com/page-1/ [R=301,L]
RewriteRule ^anotherpage/run/$ https://www.newdomain.com/anotherpage/run/ [R=301,L]
# rewrite all not custom matched requests to https://www.newdomain.com/subdirectory/
RewriteRule ^(.*)$ https://www.newdomain.com/subdirectory/ [R=301,L]
</IfModule>

redirect certain pages to its equivalent in another domain, otherwise redirect to its homepage

I want to use htaccess to redirect certain pages to its equivalent in another domain, other pages redirect to homepage.
Like that:
http://old-domain.com/certain-page redirected to: http://new-domain.com/certain-page
http://old-domain.com/another-certain-page redirected to: http://new-domain.com/another-certain-page
Only these pages will be redirected, otherwise, pages have to be redirected to the new domain home page.
http://old-domain.com/non-certain-page redirected to: http://new-domain.com
This is my try:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^certain-page$ http://new-domain.com/certain-page [R=301,L]
RewriteRule ^another-certain-page$ http://new-domain.com/another-certain-page [R=301,L]
</IfModule>
But don't know how to exclude other pages.
Any help here?
Thanks in advance!
I'm not sure what you mean by
But don't know how to exclude other pages
But you are already excluding the pages when you create specific rules for them. This should work.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
#redirect specific pages
RewriteRule ^certain-page$ http://new-domain.com/certain-page [R=301,L]
RewriteRule ^another-certain-page$ http://new-domain.com/another-certain-page [R=301,L]
#Redirect everything else to homepage
RewriteRule ^(.*)$ http://new-domain.com/ [R=301,L]
</IfModule>
Let me know how this works for you. If you need to clear you cache before trying these new rules.
Another answer was mentioned here, thank to anubhava
With redirect without need to use Rewrite :
RedirectMatch 301 ^/.+ http://www.newdomain.com/
So, the answer can be also:
#Redirect specific pages
redirect 301 /certain-page http://new-domain.com/certain-page
redirect 301 /another-certain-page http://new-domain.com/another-certain-page
#Redirect everything else to homepage
RedirectMatch 301 ^/.+ http://new-domain.com

Redirect with apache

I have a website with about 2000 links, and other site with the exact same pages.
I need to know how to do in apache the redirect from one domain to the other one, taking into accounts that after the .com the page will be the same on both sites
Example
User request:
www.mydomain.com/product1
Should redirect
www.mydomain2.com/product1
It should be a 301 redirect
Thank you for your help
This can easily be done with mod_rewrite:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
# redirect all requests to www.domain2.com domain
RewriteCond %{HTTP_HOST} =www.domain.com
RewriteRule .* http://www.domain2.com%{REQUEST_URI} [R=301,L]
This to be placed in .htaccess in website root folder (or into Virtual Host context) for www.mydomain.com. This also assumes that www.mydomain.com and www.mydomain2.com are on different servers / virtual hosts (in other words, not pointing into the same physical folder).
I do this at my company all the time. If you know how to use mod_rewrite, turn RewriteEngine on and use a RewriteRule with the [R] flag:
RewriteRule www.mydomain.com/product1 www.mydomain2.com/product1 [L,R]
See this site.

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]