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.
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.
Ok, one problem right away, you aren't specifying a target domain on your rewrite rule. If all requests from any subdomain except www need to be redirected, you should specify that like:
RewriteCond %{HTTP_HOST} !^www\.domain\.com [NC]
RewriteCond %{HTTP_HOST} ^((www\.)?([a-z]+)\.)domain\.com [NC]
RewriteRule .? http://www.domain.com/index.php?id=%3 [L]
The other problem with this is that you are ignoring the file path after foo.domain.com, so that ALL requests get redirected to domain/index.php?id=foo regardless if the requested file was a page or an image.
A better way might be to give each "subdomain" its own folder, something like
RewriteCond %{HTTP_HOST} !^www\.domain\.com [NC]
RewriteCond %{HTTP_HOST} ^((www\.)?([a-z]+)\.)domain\.com [NC]
RewriteRule ^(.*)? http://www.domain.com/%3/$1 [L]
so a path like foo.domain.com/content/imgs/logo.jpg would point to domain.com/foo/content/imgs/logo.jpg
Related
The goal
Visting www.DOMAIN.com/blog to load Wordpress from a directory named "/wp-blog" or similar. Also another rule is the host must match a specific domain.
Current rules
This does not seem to be working:
RewriteCond %{REQUEST_URI} ^blog [NC]
RewriteCond %{HTTP_HOST} ^(www.)?DOMAIN.com$
RewriteRule ^(/)?$ wp-blog [PT,L]
Any ideas what we're doing wrong?
The fix was actually simple
The rules just needed altering slightly and I had to actually change the /blog directory name as some other rules were causing apache to get confused.
RewriteCond %{REQUEST_URI} ^/blog [NC]
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$ [NC]
RewriteRule ^blog(/.*)?$ /wp_blog/$1 [L]
Also had to update htaccess for the Wordpress install as well to reference wp_blog
I beg you pardon if this is a duplicate question. What I've found on the net didn't satisfy me at all.
As I asked in this question, I successfully rewrote the /public folder to the root url. So If I visit localhost or localhost/public apache redirects me to /public and hides public in the url. To sum up, I did it in this way:
RewriteEngine on
RewriteCond %{THE_REQUEST} /public/([^\s]+) [NC]
RewriteRule ^ /%1 [NC,L,R]
RewriteCond %{REQUEST_URI} !^/public
RewriteRule ^(.*)$ /public/$1 [NC,L,QSA]
However in this way everything will be redirected to the subfolder /public. I need to preserve, or to add some exceptions, to some subdirectories that are not in the /public folder.
+---assets
+---images
\---jp-pattern.jpg
+---javascripts
+---stylesheets
+---template
+---public
\---products
I need to preserve assets, images, javascripts, stylesheets, and template folders. I need them to be for example localhost/images and so on.
I tried with the images folder first in this way:
RewriteCond %{REQUEST_URI} !^/images [NC]
or
RewriteCond %{REQUEST_URI} !^/images/?$ [NC]
or
RewriteCond %{REQUEST_URI} !/images [NC]
but none of them worked. The browser always returns me this message:
Not Found
The requested URL /public/images/jp-pattern.jpg was not found on this server.
This means that I couldn't add the exception.
How can I add those exceptions?
You can add this block directly after RewriteEngine on:
RewriteCond %{REQUEST_URI} ^/(images|javascripts|stylesheets|template)/ [NC]
RewriteRule ^ - [L]
This says that if the request begins with either of those directories, don't rewrite. Use of the L flag makes that the last rule to be checked.
However, I advise that you actually do an existence check instead. Your HTML would likely be requesting an existing file, and so it would skip the public check anyway.
RewriteCond %{REQUEST_URI} !^/public
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /public/$1 [NC,L,QSA]
I want to redirect the the subdomain to subdirectory but not working. Here is my efforts.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^name\.site_url
RewriteRule ^(.*)$ http://site_url/name/$1 [L,R=301]
Try this:
RewriteCond %{HTTP_HOST} ^sub\.
RewriteRule ^(.*)$ http://your_domain/sub/$1 [R,L]
Instead sub in RewriteCond and RewriteRule you can place whatever you want
Btw, RewriteCond %{HTTP_HOST} ^sub\.my\.domain$ also works for me. So, check your site url. Or give more info (at least, what happened in apache error.log).
I want to write a RewriteRule and RewriteCond, so that whenever sub domain is called different file is going to processed, with same url.
I have tried by following code, but's not working for me
RewriteCond %{HTTP_HOST} !^mydomain\.test\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.mydomain\.test\.com$ [NC]
RewriteRule ^$ /app/test/index.php?param=$1 [NC,QSA,L]
I have configured all other steps properly. Just every time its calling index file from www, not from mentioned folder.
You're not capturing $1 in your RewriteRule, change your code to this:
RewriteCond %{HTTP_HOST} ^([^.]+)\.mydomain\.test\.com$ [NC]
RewriteRule ^((?!app/test/index\.php).*)$ /app/test/index.php?param=$1 [QSA,L,NC]
I am building a php site framework and there are a couple of things that I would like to add in out of the box but they are going to need to be handled by .htaccess
UPDATED:
I am so close to getting this working perfectly.
RewriteEngine on
#this rule removes www from the URL if its used
RewriteCond %{HTTP_HOST} ^www.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
#this rule handles the subdomains
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([a-z0-9]+)\.(.*)$ [NC]
RewriteRule ^(.*)$ index.php[L]
#this rule handles redirecting all addresses to index.php
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ index.php
That is all working now it handles the subdomains and all paths at the address bar.
The one thing that is occuring now though is that if I try to access a file directly for example css/master.css with an absolute path then it loads index.php instead.
Is there a tweak I can do to this to make sure if a file exists at the path that is does not redirect.
So close
Testing site.
http://something.rtbstats.com (404: as subdomain not there)
http://tracking.rtbstats.com (uses the index file from subfolder)
http://rtbstats.com (root)
http://rtbstats.com/home (loads homepage)
http://admin.rtbstats.com (pulls the admin area up)
I am only 1 level deep on the prettyURLs for this site but plan to build the admin area out to facilitate categories sub cats etc in the url path.
The admin area will even manage subdomains now with no need to access cpanel.
All of those different URLS are all handled by the .htaccess all I need now is to fix the absolute paths to files not working.
What about the dynamic loading of the domain to replace yoursite.com
Like this:
#this rule removes www from the URL if its used
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTP_HOST} ^www.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
#this rule handles the subdomains
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([a-z0-9]+)\.(.*)$ [NC]
RewriteRule ^(.*)$ index.php?/public_site/%1/$1 [L]