ipel
ipel

Reputation: 1348

multiple domains redirect with htaccess

i have a website, example: mydomain.com

then i have other domains, for example: mydomain.org, mydomain.net,.. set with cpanel addon domains function the on directory /domain/ with an index.php file inside

in this way when a user go on mydomain.net or .org is redirected from the /domain/index.php file on the main website (mydomain.com)

eveything work fine except if a user type mydomain.net/somefile or mydomain.net/somedir/somefile, in these cases the user get an 404 error message

my question is: there is a way (with htaccess) to remove everything from the url exept the domain (in this way if a user type mydomain.net/somefile or mydomain.net/somedir/somefile is redirected normally by the /domain/index.php file)?

Upvotes: 1

Views: 779

Answers (1)

Ravi K Thapliyal
Ravi K Thapliyal

Reputation: 51711

Add this to your .htaccess in your web root / directory

RewriteEngine on
RewriteBase /

RewriteCond %{HTTP_HOST} !^(www\.)mydomain\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-d # not a dir
RewriteCond %{REQUEST_FILENAME} !-f # not a file
RewriteRule ^ domain/index.php [L]

This intercepts all 404s for any domain (except mydomain.com) and serves the request from /domain/index.php. If you'd like the address bar URL to change as well; say, from mydomain.net/somefile to mydomain.net use the redirection [R] flag as

RewriteRule ^ http://%{HTTP_HOST}/ [R=301,L]


/domain/.htaccess :

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-d # not a dir
RewriteCond %{REQUEST_FILENAME} !-f # not a file
RewriteRule ^ http://%{HTTP_HOST}/ [R=301,L]

%{HTTP_HOST} condition was dropped because only non .com domains are mapped to /domain.

Upvotes: 3

Related Questions