views:

33

answers:

1

How can I redirect my users from example.com / or www.example.com to new.example.com

but I dont want to redirect some specific urls like:

www.example.com/test.php
api.example.com/testo.php
www.example.com/forum/index.php

I did:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule ^(.*)$ http://new.example.com/$1 [R=301,L]

but what is the rules for urls?

A: 

One way to do this is to create additional rules earlier in the .htaccess file for the specific URLs that just redirect to themselves (internally, not using a 3XX response), and then use the L flag so no later rules are processed for those URLs.

For example:

RewriteEngine on

# don't redirect these
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule ^(/test.php)$ \1 [L]

RewriteCond %{HTTP_HOST} ^api.example.com$
RewriteRule ^(/testo.php)$ \1 [L]

RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule ^(/forum/index.php)$ \1 [L]

# redirect these
RewriteCond %{HTTP_HOST} ^example.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule ^(.*)$ http://new.example.com/$1 [R=301,L]

I'm not sure if your "don't redirect" requirements included the hostname. If not, then just remove the RewriteCond lines.

Laurence Gonsalves
buy how can I do that?
gidon
I just added some example code.
Laurence Gonsalves