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

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

Related

.htaccess - redirect, if a specific url match

I need to redirect users to a new domain, but I need that in case a user visits a particular URL, then he/she needs to be redirected to another URL.
Let me clarify...
If the user visits http://oldexample.com/postvendita/ I need to redirect them to http://newexample.com/assistenza
Otherwise, for every other URL http://oldexample.com/* I need to redirect them to http://newexample.com/new-page
Here is my attempt:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)postvendita(.*)$ http://www.newexample.com/assistenza [L,R=301]
RewriteCond %{HTTP_HOST} !^oldexample\.com$ [NC]
RewriteRule ^(.*)$ http://www.newexample.com/new-page [R=301,L]
Now if I visit any of the old pages, I will be redirected to http://www.newexample.com/new-page, so the first rule doesn't work, how should I change it?
To handle this via .htaccess you'll want to match the first one and use a catch-all to redirect everything else:
RewriteEngine On
RewriteRule ^postvendita/?$ http://www.newexample.com/assistenza [R=301,L,NE]
RewriteRule .* http://www.newexample.com/new-page [R=301,L,NE]
A 301 redirect is a permanent redirect which passes between 90-99% of
link juice (ranking power) to the redirected page. 301 refers to the
HTTP status code for this type of redirect. In most instances, the 301
redirect is the best method for implementing redirects on a website.
More About Redirects
Alternatively, if you're not comfortable writing RewriteRules, you can use the following lines:
Redirect 301 /postvendita/ http://www.newexample.com/assistenza
Redirect 301 /postvendita http://www.newexample.com/assistenza
Redirect 301 / http://www.newexample.com/new-page
Or with RedirectMatch:
RedirectMatch 301 /postvendita/? http://www.newexample.com/assistenza
RedirectMatch 301 .* http://www.newexample.com/new-page
Common .htaccess Redirects

htaccess RewriteRule and Redirect 301 don't work

We're trying to set up 301 redirects to make sure Google uses the right category pages. We can't set up a canonical and a normal Redirect 301 /n.html https://website.com/n.html doesn't work either, it's just ignored. We're on Opencart 1.5.5.1. The issue is that the 301s are just ignored, they're not implemented at all both Redirect 301 and RewriteRule
# SEO URL Settings
RewriteEngine On
RewriteRule ^/refrigeration/Multidecks/fresh-meat-multidecks$ https://www.website.co.uk/Multidecks/fresh-meat-multidecks [R=301,L]
RewriteRule ^/fresh-meat-multidecks$ https://www.website.co.uk/Multidecks/fresh-meat-multidecks/ [R=301,L]
Like Anubhava said, the issue is the / after the ^

Redirecting old site / subfolders to new site / subfolders

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>

Access webpage from subdirectory as root page

I have two websites:
http://example.com
http://example.com/sub
I have a page, let's say:
http://example.com/sub/page1
Is there any way (using .htacces) to make it possible to access page1 via this url:
http://example.com/page1?
Redirect 301 won't do the work, because I don't need redirects. Is there any way to omit '/sub/' for certain urls, like http://example.com/sub/page1 ?
Yep, there is. Do the following within your .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
# This would rewrite any URL that starts with /sub to /
RewriteCond %{REQUEST_URI} /sub
RewriteRule ^sub/(.*)$ /$1 [L]
<IfModule>
Edited to meet the OP's requirements.

Redirect using .htaccess

I want to redirect all user page requests to a page on the same domain.
For example, I have an "under construction, BRB" page that I want all users to see when they try to access ANY page on the site.
I tried using this:
Redirect 302 / http://www.domain.com/index2.php
What that does is try to apply the redirect to the index2.php page as well and it gets stuck in a loop where the user then sees this until the browser stops.
http://www.domain.com/index2.phpindex2.phpindex2.phpindex2.php etc., etc,
Any idea on how to write that rule to except that page?
You have to exclude the file you want to redirect to. Here’s an example with mod_rewrite:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule !^index2\.php$ /index2.php [L,R=302]
</IfModule>
To prevent endless looping (index2.php <--> index2.php):
Only redirect If URL to be redirected does NOT contain the string 'index2.php'
RewriteCond %{QUERY_STRING} !index2.php
RewriteRule ^(.*)$ /index2.php [L,R=301]
You could use mod_rewrite
<IfModule mod_rewrite.c>
RewriteCond {REQUEST_URI} !=/index2.php
RewriteEngine on
RewriteRule ^.*$ /index2.php
# End .HTACCESS
</IfModule>
I'd be more inclined to use the slightly different
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^/.*$ /index2.php [R=307,L]
</IfModule>
This will let you return a moved temporarily status for the redirect.
Omitting this set of flags means that mod_rewrite will return a 302 Found status by default.
HTH
cheers,