Christian Giupponi Christian Giupponi - 7 days ago 6
Apache Configuration Question

htaccess - redirect if url match

I need to redirect users to a new domain but I need that in case a user comes from a particular url he/she need to be redirect to a spcific url.

Let me clarify...

If user visit

http://oldexample.com/postvendita/
I need to redirect him 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 my try:

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]


If I visit every old page I will be redirect to
http://www.newexample.com/new-page
so the first rule doesn't work, how should I change it?

Answer

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