Christian Giupponi
Christian Giupponi

Reputation: 7618

.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?

Upvotes: 1

Views: 9094

Answers (1)

Daerik
Daerik

Reputation: 4277

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

Upvotes: 2

Related Questions