Forum Moderators: phranque

Message Too Old, No Replies

htaccess redirect all other domains to single domain with URI intact

         

bubba1356

8:00 pm on Sep 1, 2010 (gmt 0)

10+ Year Member



Hi guys,

I'm having trouble setting up htaccess to redirect all other domains to a single domain whilst passing the URI.

I have a number of domains pointing to the same directory so all execute the same .htaccess file when requested, and wanted to do a conditional redirect instruction for all other domains.

Say I own example1.com, example2.com, example3.com (all serving from the same directory on the server) and want to redirect all to newexample.com- this I can do no problems. However I want requests to example1.com/somepage to resolve at newexample.com/somepage
(same applies to other domains).

Here's what I've been working with:

RewriteCond %{HTTP_HOST} !^newexample.com/$
RewriteRule (.*) http://newexample.com/$1 [R=301,L]


I'm having difficulty passing the URI in the redirect- anyone know how to do this?


Thanks!

jdMorgan

9:15 pm on Sep 1, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This shouldn't work at all, since %{HTTP_HOST} contains only the hostname, plus an optional trailing port number. It won't contain any part of the URL-path, such as the initial URL-path slash. So if you're "having trouble passing the URL," then be advised that you've got some other redirect code somewhere that is doing the (faulty) redirect -- This one here is essentially non-functional. The other code will be in any .htaccess file along the directory-path to which the requested URL resolves, or in a server config file (possibly written there using your "control panel").

If on the other hand, the problem is simply that no redirect is taking place at all, then fix this code:

RewriteCond %{HTTP_HOST} !^newexample\.com$
RewriteRule ^(.*)$ http://newexample.com/$1 [R=301,L]

I assume that you've got other working rules in this .htaccess file, and that the one or two required 'set-up' directives are present to allow you to use mod_rewrite. If not, then you will need either both of these lines or only the second one placed ahead of the code above; you have to test to find out whether the first line is needed and allowed:

Options +FollowSymLinks
RewriteEngine on

Delete or diable your browser cache before testing any new server-side code. "Forcing a reload" is often insufficient.

Jim

bubba1356

6:49 am on Sep 2, 2010 (gmt 0)

10+ Year Member



Thanks for your reply Jim. I tested your code and you're right- this is clashing with another statement in the .htaccess file:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On

RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

RewriteCond %{HTTP_HOST} !^example\.com$
RewriteRule (.*) http://example.com/$1 [R=301,L]
</IfModule>

# END WordPress

Commenting out the WordPress instructions at the top works fine, although the page doesn't load as index.php isn't called. Any ideas how I can combine this with the standard WordPress .htaccess code?

Thanks

jdMorgan

1:01 pm on Sep 2, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



See Proper Order for htaccess directives [webmasterworld.com] and Reducing Costly Rewrites in Wordpress [webmasterworld.com] in our Apache Forum Library. Both of these threads contain information that I believe you will find quite useful.

Jim