Christian Giupponi Christian Giupponi - 1 year ago 142
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
I need to redirect him to

Otherwise, for every other url*
I need to redirect them to

Here my try:

Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)postvendita(.*)$ [L,R=301]
RewriteCond %{HTTP_HOST} !^oldexample\.com$ [NC]
RewriteRule ^(.*)$ [R=301,L]

If I visit every old page I will be redirect to
so the first rule doesn't work, how should I change it?

Answer Source

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/?$ [R=301,L,NE]
RewriteRule .*       [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/
Redirect 301 /postvendita
Redirect 301 /   

Or with RedirectMatch:

RedirectMatch 301 /postvendita/?
RedirectMatch 301 .*   

Common .htaccess Redirects

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download