Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wildcard Sub Domain Path Issues

I have an htaccess rewrite for wildcard sub domains which rewrites foo.domain.com to domain.com/index.php?id=foo. This is done using:

RewriteCond %{HTTP_HOST} !^www\.domain\.com [NC]
RewriteCond %{HTTP_HOST} ^((www\.)?([a-z]+)\.)domain\.com [NC]
RewriteRule .? index.php?id=%3 [L]

This works OK, but all content on the site is referenced to the root e.g:

 "/content/imgs/logo.jpg" or "/ajax/upload.php"

The wild-card sub domain changes the root and all content is referenced to:

"http://foo.domain.com/content/imgs/logo.jpg"

And the content cannot be found because it is not located on this subdomain.

I know you can use the html < base > tags to place a prefix on all href locations but this does not help in javascript for ajax requests.

Is there anything that can be done in htaccess to solve this problem?

Thank you.

like image 266
Kit Avatar asked Jan 30 '26 19:01

Kit


1 Answers

I think I may have the solution to your problem Christopher!

Your rewrite condition was being applied to ALL included content such ass css/js/images etc. Which mean it was trying to rewrite this content to index.php?id=%3 instead of style.css.

By adding the line RewriteCond %{REQUEST_FILENAME} !-f above the condition and placing the entire condition at the bottom of the htaccess means that it will only apply to files/directories that DO NOT EXIST (i.e. only to wildcard subdomains).

Try the following:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTP_HOST} !^www\.domain\.com [NC]
RewriteCond %{HTTP_HOST} ^((www\.)?([a-z]+)\.)domain\.com [NC]
RewriteRule ^(.*)$ "http://domain.com/index.php?id=%3" [L,P]

Hope this helps you mate!

W.

like image 168
wilsonpage Avatar answered Feb 02 '26 09:02

wilsonpage



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!