Forum Moderators: phranque

Message Too Old, No Replies

Rewrite: Point external domain to folder while keeping original url

         

chez17

3:11 am on Apr 9, 2009 (gmt 0)

10+ Year Member



Hello all, I could use some help with apache's rewrite. Basically I have an external domain pointing to a single server. So I have externaldomain.com pointing to homedomain.com. I would like externaldomain.com point to a folder on homedomain.com while keeping externaldomain.com in the address bar. I have the condition down, but I can't get the rule to work.

RewriteEngine on
RewriteCond %{HTTP_HOST} ^externaldomain\.com$

That works fine, I have tried all sorts of variants on the rule. If I do this:

RewriteRule ^(.*)$ folder/$1 [R=301,L]

it displays the homepage of homedomain while keeping externaldomain in the url, but it isn't redirecting to the folder I want it to. If I do this:

RewriteRule ^(.*)$ /folder/$1 [R=301,L]

it redirects to the proper folder but the url changes from externaldomain to homedomain. I have looked all over this site but can't seem to figure this one out. I have tried google but one issue is I don't know exactly what to look for or what the proper term for what I am doing is. Any help is most appreciated. Thanks.

jdMorgan

5:44 am on Apr 9, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You want an internal (URL-to-filepath) rewrite, not an external (URL-to-URL) redirect. Also, you must explicitly prevent an 'infinite' rewriting loop if this code is going into an .htaccess file:

RewriteEngine on
#
RewriteCond %{HTTP_HOST} ^(www\.)?externaldomain\.com\.?(:[0-9]+)?$
RewriteCond $1 !^folder/
RewriteRule ^(.*)$ /folder/$1 [L]

The extra stuff in the RewriteCond prevents your rule from being defeated if the www subdomain is requested, or if an FQDN is used or a port number is appended. All of these possibilities are valid, but would break your original code.

The second RewriteCond prevents recursively rewriting a request for "/x" to /folder/x to /folder/folder/x to /folder/folder/folder/x, etc.

The RewriteRule syntax is modified to specify an internal rewrite.

Jim

chez17

3:08 pm on Apr 9, 2009 (gmt 0)

10+ Year Member



Jim, thanks for the help. I am still experiencing issues with the code you suggested. For example if I use it as you gave it to me, I get this error:

The requested URL /folder/ was not found on this server.

The folder is definitely on the server so it just can't find it. So what I did next was try to use an absolute path in the rewrite rule and changed it to something like this:

RewriteRule ^(.*)$ /var/www/html/folder/$1 [L]

and when I do that I get this error:

The server encountered an internal error or misconfiguration and was unable to complete your request.

Just for testing purposes I have /var/www/folder and /var/www/html/folder both set up and it's not finding either one. The .htaccess file is in the /var/www/html/ directory. I'll keep looking around the web, any help you can provide is most appreciated.

jdMorgan

3:35 pm on Apr 9, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Look at your server *error* log, and find the discrepancy between the failing filepath and the actual filepath. Your access log talks about URL-paths, but the error log will show the filepaths to which those URL-paths resolved.

Also, I have presumed that your code is in example.com/.htaccess. If it is in a server config file or in a different location, please let us know, as this makes a big difference.

Jim