How do I use mod_rewrite to alter which file is fetched based on the URL's sub-domain? - apache

So say I have a URL being requested:
sub.domain/file.bin
and I wanted this file to be fetched:
domain/hosted/sub/file.bin
How would I structure the rewrite directives? I've been working on this for days, and it just gives me a headache...
The sub-domains are numerous and are added and removed often, so I'm trying to find a solution that works for all of them.

In sub.domain's httpd config:
RewriteCond %{HTTP_HOST} ([^\.]+).domain
RewriteRule ^/file.bin$ http://domain/hosted/%1/file.bin

Assuming the home directory for www.domain.com is domain/ and that - given any subdomain sub.domain.com - you want the files for the home directories for that sub-domain to be in domain/hosted/sub/
Try something like this:
RewriteEngine on
// If the host is just mydomain.com, do nothing more
// this is to prevent some recursion problems I've read of...
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com$ [NC]
RewriteRule ^.*$ - [L]
// Otherwise strip off everything before mydomain
// And add it to the start of the request
RewriteCond %{HTTP_HOST} ^(.*?)\.(www\.)?mydomain\.com$ [NC]
RewriteRule ^.*$ %1%{REQUEST_URI} [QSA]
// Then prefix with 'hosted'
RewriteRule ^(.*)$ hosted/$1 [QSA,L]
You may also need a wildcard entry in your DNS or something... but I will admit DNS and htaccess mod rewrite are some of my weaker points. See also http://www.webmasterworld.com/forum92/138.htm

Try this rule:
RewriteCond %{HTTP_HOST} ^([^/.]+)\.example\.com$
RewriteCond %1 !=www
RewriteRule !^hosted/ hosted/%1%{REQUEST_URI} [L]

Related

I want to use.htacess to re-direct only one file

There's a file that I have that needs to be accessible to an outside source via http. My whole site is https. If I do the following:
# Redirect all http traffic to https, only if it is pointed at specialfile.txt
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} ^/specialfile.txt/
RewriteRule ^(.*)$ http://example.com/specialfile.txt$1 [R=301,L]
Does it look like this would work. I'm an absolute noob when it comes to Apache re-write rules, and I would RTFM but I need this change pretty quick.
I should also mention that there is already
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
in the .htaccess file along with some Wordpress stuff.
If you put something like the following in the root folder .htaccess file, it ought to work:
RewriteEngine on
RewriteCond %{REQUEST_URI} specialfile.txt$
RewriteRule ^(.+)$ http://example.com/$1 [R=301,L]
The directive translates as:
If the URI requested ends in specialfile.txt
Capture all characters (from one to any number) after https://domain.tld/
Rewrite the URI as http://domain.tld/all-captured-characters

.htaccess Redirect/Rewrite Issue

I am having a hard time figuring out how I can do this.
I want all requests to www.mydomain.com/manager to redirect to m.mydomain.com
I am trying:
# Friendly URLs for the manager
RewriteCond %{HTTP_HOST} ^(www.)?cloudcms.co$
RewriteRule ^manager/?$ http://m.cloudcms.co/ [NC,L,R]
RewriteCond %{HTTP_HOST} ^(m.|manager.)?cloudcms.co$
RewriteRule ^(/)?$ manager/ [L,QSA,NC]
However, the only real one that is properly rewriting is the second condition.
I have also tried the following
# Friendly URLs for the manager
RewriteCond %{HTTP_HOST} ^(www.)?cloudcms.co$
Redirect 301 /manager http://manager.cloudcms.co
RewriteCond %{HTTP_HOST} ^(m.|manager.)?cloudcms.co$
RewriteRule ^(/)?$ manager/ [L,QSA,NC]
But it ignores the second rewrite
As per my comment above:
Is there another .htaccess in /manager folder or any other in present .htaccess`
I suspect OP has another .htaccess somewhere inside your DOCUMENT_ROOT. Try removing that and this simple redirect will start working.
OP asked:
How would I be able to do this for any domain that could come in and
try hitting the /manager folder?
Just change your first redirect rule to:
# if not already on mobile domain
RewriteCond %{HTTP_HOST} !^m\. [NC
RewriteRule ^manager/?$ http://m.cloudcms.co/ [NC,L,R=301]
You probably don't want to P flag, which is reverse proxying for you. Try replacing that with an R:
RewriteRule ^manager/?$ http://m.mydomain.com/ [NC,L,R]

Subdomain to subfolder in Apache

I am looking for Apache configuration that would allow for the following:
Serve sub.domain.com as /%docRoot%/domain.com/sub, and
Would be able to do this for every hosted domain and any subdomain (ie. no per-domain vhost configuration)
I would be grateful for any solution, especially if there was no mod_rewrite involved (using mod_vhost_alias).
Note: There are some obvious solutions using mod_vhost_alias, but they either work for domain.com or sub.domain.com, none of them seems to cover both cases.
Have a nice day!
Point *.domain.com to your document root (/%docRoot%/). You'll need to do this in a vhost config. In that same vhost, add this:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/domain.com/
RewriteCond %{HTTP_HOST} ^([^\.]+)\.domain\.com$ [NC]
RewriteRule ^/(.*)$ /domain.com/%1/$1 [L]
If you want to avoid pointing www.domain.com to /%docRoot%/domain.com/www, then add a condition to exclude it:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/domain.com/
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^([^\.]+)\.domain\.com$ [NC]
RewriteRule ^/(.*)$ /domain.com/%1/$1 [L]
EDIT:
I assume I will still have to do this for every hosted domain (as the examples you've posted all refer to "domain.com"). Am I right?
Yes, the above will only do the routing for domain.com, if you want to do this for all arbitrary domain.com, you'll need to do something a bit more tricky:
RewriteEngine On
# here, %1 = subdomain name, and %2 = domain name
RewriteCond %{HTTP_HOST} ^([^\.]+)\.(.+)$ [NC]
# make sure the request doesn't already start with the domain name/subdomain name
RewriteCond %{REQUEST_URI}:%2/%1 !^/([^/]+/[^/]+)[^:]*:\1
# rewrite
RewriteRule ^/(.*)$ /%2/%1/$1 [L]
The tricky thing here is the %{REQUEST_URI}:%2/%1 !^/([^/]+/[^/]+)[^:]*:\1 match. It posits the condition: %{REQUEST_URI}:domain.com/sub and makes sure the %{REQUEST_URI} doesn't start with the domain.com/sub (back refrenced from previous match using %2/%1) using the \1 back reference.
With this, you setup your vhost to accept every domain (default vhost) and any subdomain/domain will get routed. Examples:
http://blah.bleh.org/file.txt goes to /%docRoot%/bleh.org/blah/file.txt
http://foo.bar.com/some/path/ goes to /%docRoot%/bar.com/foo/some/path/
http://sub2.sub1.d.com/index.html goes to /%docRoot%/sub1.d.com/sub2/index.html
EDIT2:
Yes, I would very much like to get domain.com routed to /%docRoot%/domain.com/
Try these:
RewriteCond %{HTTP_HOST} ^(.+?)\.([^\.]+\.[^\.]+)$ [NC]
RewriteCond %{REQUEST_URI}:%2/%1 !^/([^/]+/[^/]+)[^:]*:\1
RewriteRule ^/?(.*)$ /%2/%1/$1 [L]
RewriteCond %{HTTP_HOST} ^([^\.]+\.[^\.]+)$ [NC]
RewriteCond %{REQUEST_URI}:%1 !^/([^/]+)[^:]*:\1
RewriteRule ^/?(.*)$ /%1/$1 [L]
Basically the same thing, except a few of the regex needs to be tweaked to separate what a domain.com is like vs sub.domain.com. If you want to redirect www.domain.com to domain.com, that needs to happen before these rules.

.htaccess rewrite to simultaneously change domain and remove path

My URL structure is currently as follows:
http://domain.com/folder/filename (CURRENT)
I want to change this so that I can use the following URL instead:
http://sub.domain.com/filename (NEW)
So accessing the CURRENT or the NEW url, should load the file located at the CURRENT url, but show the NEW url in the address bar. It should only apply to the "/folder/" path.
sub.domain.com is a mirror of domain.com, ie. they share the same file system and root directory.
This is what I have so far:
Options +FollowSymLinks
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$ [NC]
RewriteCond %{REQUEST_URI} ^/folder/?(.*)$ [NC]
RewriteRule ^(.*)$ http://sub.domain.com/$1 [R=301,L]
This is working, but is missing the rule to remove the "/folder/" from the path. I've tried combining multiple RewriteRule's with no luck. Any ideas? Thanks.
UPDATE: Thanks again #Gerben - I understand what your rules are doing now, but the second one isn't working for me. I suspect because it's conflicting with some other rewrite rules, in particular those of WordPress, which are lower down in my .htaccess file:
# BEGIN WordPress
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress
Because of this the page ends up in a redirect loop, ie (from Chrome):
"The webpage at http://sub.domain.com/folder/index.php has resulted in too many redirects." - while the url I was originally trying to access was, for example, http://sub.domain.com/page
Any ideas?
Try:
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$ [NC]
RewriteRule ^(folder/)?(.*)$ http://sub.domain.com/$2 [R=301,L]
This will redirect everything to sub.domain.com, and remove the /folder part of the URI if it is there. If not, it redirects and leaves the URI untouched.
RewriteCond %{THE_REQUEST} /folder/
RewriteRule ^folder/(.*)$ http://sub.domain.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^sub\.domain\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/folder/
RewriteRule ^(.*)$ folder/$1 [L]
# WordPress rules here
edit the second R=301 should not have been there
But this won't work, as wordpress has no way of knowing you want folder. You could add the Proxy flag to the rewrite, but then you need to change the rule above to not redirect on this internal proxy request.

How can I change all subdomains to read different content from their respective subdirectories?

I have my DNS configured to accept any subdomain (wildcard *), but I am having trouble feeding back the required content to the browsers.
I would like each subdomain to return the relative content, which resides in subdirectories of the same name within the public_html path of my server.
eg, example.domain.com/picture.jpg would actually request the file at public_html/example/picture.jpg
Currently I have tested the followed .htaccess code, but it is not functional:
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$ [NC]
RewriteRule ^(.*)$ %1/$1 [L]
This code, and similar tests, can redirect based on the subdomain (%1) fine, but the request string ($1) seems to be the issue.
Maybe you could take a look at the mod_vhost_alias module :
http://httpd.apache.org/docs/2.0/mod/mod_vhost_alias.html
Try the following:
RewriteCond %{HTTP_HOST} ^([0-9a-z-]+).domain.com [NC]
RewriteRule ^/(.*) http://domain.com/%1/$1 [L]
I didn't test this one but I use similar rules for proxying:
RewriteCond %{HTTP_HOST} !^domain.local [NC]
RewriteCond %{HTTP_HOST} ^([0-9a-z-]+).domain.com [NC]
RewriteRule ^/(.*) http://domain.local/%1/$1 [P]