.htaccess - redirect, if a specific url match - apache

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

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>

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 ^

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

Htaccess www redirect - status codes

i am using this rewrite rule in my htaccess:
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
basically it redirects the user to www if he type the adress without the www. (sub-domins excluded).
Is there any other way to do it without htaccess file?
My Problem:
If i navigate the Browser to my Page The first HTTP-Satus code i get is the (www) redirect 301 and then the 200 (for the www.domain.com).
If i try this with, for example. ebay.de the first status code i get is "Pending" and then i get redirected to www.ebay.de with 200.
How they do the www redirect without a 301 htaccess?

Remove query strings from HTTP response status code 301 url redirections

My .htaccess file contains Rewrite URLs and HTTP response status code 301 url redirections. When I test the redirections, it adds the query string values from the old url to the end of the redirected url. How can I stop this?
My htaccess looks like this.
Options +FollowSymlinks
Redirect 301 /old-site/tees~1-c/blue~123-p.html test.mydomain.com/tees~1-c/blue~123-p.html
Redirect 301 /old-site/tees~1-c.html test.mydomain.com/tees~1-c.html
Redirect 301 /old-site/content/about.html test.mydomain.com/content/about.html
RewriteEngine on
RewriteRule ^(.+)~(.+)-c/(.+)~(.+)-p\.html?$ product.php?cde=$1&cid=$2&pde=$3&pid=$4 [QSA]
RewriteRule ^(.+)~(.+)-c\.html?$ category.php?cde=$1&cid=$2&ref=%3&srt=%4&sta=%5&ppa=%6 [QSA]
RewriteRule ^content/(.+)\.html?$ info.php?seo=$1&sta=%1 [QSA]
As stated at the mod_alias doc
If the client requests http://example.com/service/foo.txt, it will be
told to access http://foo2.example.com/service/foo.txt instead. This
includes requests with GET parameters, such as
http://example.com/service/foo.pl?q=23&a=42, it will be redirected to
http://foo2.example.com/service/foo.pl?q=23&a=42
You can change your Redirec 301 to Rewrite rules:
Instead of:
Redirect 301 /old-site/tees~1-c/blue~123-p.html test.mydomain.com/tees~1-c/blue~123-p.html
Redirect 301 /old-site/tees~1-c.html test.mydomain.com/tees~1-c.html
Redirect 301 /old-site/content/about.html test.mydomain.com/content/about.html
Use:
RewriteRule /old-site/tees~1-c.html test.mydomain.com/tees~1-c.html [L,R=301]
RewriteRule /old-site/tees~1-c.html test.mydomain.com/tees~1-c.html [L,R=301]
RewriteRule /old-site/content/about.html test.mydomain.com/content/about.html [L,R=301]
If you don't include the QSA flag, NO query param is added to the rewrited url