Rewrite Directory Based on Subdomain - .htaccess

I have a folder export that is accessible to all my subdomains:
/export/sub1/...
/export/sub2/...
/export/sub3/...
Right now, regardless of what subdomain you're on, you can see all of the content by changing the directory in the url.
It's not a security issue, but more of a canonicalization concern, but I'd like to use an .htaccess file to rewrite the folders so people see a modified path that matches up with their subdomain:
sub1.domain.tld/export/... is served from /export/sub1/...
sub2.domain.tld/export/... is served from /export/sub2/...
sub3.domain.tld/export/... is served from /export/sub3/...
How can I do this?

You can use this generic rule in site root .htaccess:
RewriteEngine On
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} ^(?!www\.)([^.]+) [NC]
RewriteRule ^/?export/(.*)$ /export/%1/$1 [L,NC]

Related

Redirect multilevel subdomain to a folder .htaccess

I have a subdomain meet.example.com that points to the app folder and I want api.meet.example.com to point to the api folder.
The folder structure is like this
public_html - root folder
app
api
Then this is the rule that I have in my .htaccess I have in the root folder. The first rule works fine and opens the content from the app folder, but the second rule (api.meet.example.com) is not opening the content from the api folder.
RewriteCond %{HTTP_HOST} ^meet\.example\.com$
RewriteCond %{REQUEST_URI} !^/app/
RewriteRule (.*) /app/$1 [L]
RewriteCond %{HTTP_HOST} ^api\.meet.example\.com$
RewriteCond %{REQUEST_URI} !^/api/
RewriteRule (.*) /api/$1 [L]
What I am doing wrong here?
SOLUTION
Thanks for #anubhava question about the VirtualHost config I just realized that I did redirect both domains from one hosting to another, but didn't add both domains to the new hosting just the meet.example.com, assuming that as api.meet.example.com is actually a subdomain of the already added then I will not need to add it.
Adding api.meet.example.com as a domain to the new hosting fixed the issue.

htaccess configuration directories for subdomains and root domain

My folders in my www directory as set up as follows:
www/forums
www/helpdesk
www/www
In the base www folder (not www/www), I have the following set up in my htaccess file to redirect based on subdirectory
# Direct subdomains to appropriate folder in WWW directory
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
I also have the following set up for configuring the index.php on the main site (www.example.com) - this is also in the htaccess file in the www directory:
# Rewrite rules
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ www/$1 [L,QSA]
The problem I am facing is that I have stuff from forums.example.com that I want to embed in www.example.com (Vanilla Forums + WordPress plugin) - if I configure this using the admin panels, the iFrame gets blocked because they are different domains.
I found out that when I go to www.example.com/forums, I get the same front page as forums.examples.com - but all the clean URLs break.
When I look at the .htaccess file in the www/forums folder, I see the following configuration
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php\?p=$1 [QSA,L]
What do I need to do to the .htaccess file in the www directory so that when I go to www.example.com/forums, it works the same as if I went to forums.example.com
...the following set up in my htaccess file to redirect based on subdirectory
# Direct subdomains to appropriate folder in WWW directory
Aside: That snippet is just a canonical non-www to www redirect. It doesn't "redirect based on subdirectory" nor does it "direct [any] subdomains". (?)
the iFrame gets blocked because they are different domains.
It sounds as if you need to set an Access-Control-Allow-Origin HTTP response header on forums.example.com to allow the content to be "embedded" in example.com? Something like the following (using mod_headers) in the www/forums/.htaccess file:
Header set Access-Control-Allow-Origin "http://example.com"
What do I need to do to the .htaccess file in the www directory so that when I go to www.example.com/forums, it works the same as if I went to forums.example.com.
How do "all the clean URLs break"? However, given the HTTP header mentioned above, you shouldn't have to do anything more and still access forums.example.com (not the subdirectory).
Incidentally, the fact you can access the subdirectory (that the subdomain points to) is just how your hosting is configured. Normally you should block access to the subdirectory in order to prevent duplicate content issues (and any other issues, such as the linking problem you mention).
Also note, that due to the way mod_rewrite directives are inherited (or not in this case). The mod_rewrite directives in the www/forums/.htaccess file completely override the mod_rewrite directives in the parent folder.

htaccess redirect if filename equals something

I'd like to set a redirect (preferably with RewriteCond) so that if the requested file is index.php regardless of directory, it will be redirected to another site.
So visiting /index.php or/files/index.php or /stuff/index.php (etc.) will all redirect you to another domain.
Here is a general way to do it. This rule set should be placed in the .htaccess file in the root directory:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} index\.php/? [NC]
RewriteRule .* http://otherdomain.com/ [R=301,L]
Redirects permanently any URL that holds index.php in any position, like
http://mydomain.com/index.php or
http://mydomain.com/any/folder/quantity/index.php or
http://mydomain.com/any/folder/quantity/index.php/any/folder/quantity/
To
http://otherdomain.com/
That's it. You don't explain much so nothing is passed to the other domain, just as you say in your question:
...redirected to another site.
These rules should do it (when placed inside /.htaccess file):
RewriteEngine On
RewriteRule (?:^|/)index\.php$ http://otherdomain.com/ [R=301,L]

subdomain including htaccess redirect

I have a subdomain code.domain.com that redirects to domain.com/code
I want to redirect for example: code.domain.com/hello/world.html to domain.com/code/?path=hello/world.html
This is what I have so far:
RewriteEngine ON
RewriteRule ^code/(.+)$ http://domain.com/code/?path=$1
But I am not sure what is wrong with it, it doesn't seem to work.
Thanks.
Use these rules in the root directory htaccess file (not the code directory) (you can also point the code.domain.com to the root directory instead if you want) :
RewriteEngine on
RewriteCond %{HTTP_HOST} ^code.domain.com$
RewriteRule ^(.+)$ /code/?path=$1

simple subdomain set up for local development (htaccess?)

I am trying to set up a website, domain
examplesproject.co.uk
with a subdomain which is for the moment called
sub.examplesproject.co.uk .
I am with Bluehost and so I have set up the subdomain and I have got the same document root both for examplesproject.co.uk and sub.examplesproject.co.uk but I want content for
sub.examplesproject.co.uk
to actually be located at
examplesproject.co.uk/sub .
So why didn't I set up the document root at examplesproject.co.uk/sub for the sub-domain? Because then, in local development I would need to treat the two domains as completely separate and that would mean no relative urls which seemed silly given that the subdomain folder is just tantalisingly inside the main domain.
However, if someone browsed to sub.examplesproject.co.uk they would get the same content as examplesproject.co.uk, which I don't want, so I set up htaccess rewrite in the root folder like so:
#rewite sub-domain to sub directory
RewriteCond %{HTTP_HOST} ^[www\.]*sub.examplesproject.co.uk [NC]
RewriteCond %{REQUEST_URI} !^/sub/.*
RewriteRule ^(.*) /sub/$1 [L]
That works, however if you browse to examplesproject.co.uk/sub you can still see the content and I don't want to have two locations for the same content. However if I rewrite this sub-directory to show the sub-domain in the browser address, then I create a loop where it keeps feeding round.
As another side-point, I want my main site to be forced to use www, so I also have the following in my root htaccess..
#force add www on main domain
RewriteCond %{HTTP_HOST} ^examplesproject.co.uk$
RewriteRule ^(.*)$ http://www.examplesproject.co.uk$1 [R=301,L]
However, I want to force the sub domain not to have a www infront i.e. http://sub.examplesproject.co.uk and NOT http://www.sub.examplesproject.co.uk. To do this I am trying the following but it doesn't seem to work for other directories within the sub-domain (if that makes sense). Anyhows this is the code which I put in the sub directory (ie at examplesproject.co.uk/sub):
#force remove www on sub-domain
RewriteCond %{HTTP_HOST} ^www.sub.examplesproject.co.uk [NC]
RewriteRule ^(.*)$ http://sub.examplesproject.co.uk/$1 [L,R=301]
Thought I'd mention in case it affects things.
So, my question is, how do I achieve a nice set-up where:
I can use relative URLs for developing and implementing my subdomain.
browsing to http://sub.examplesproject.co.uk shows the content of http://www.examplesproject.co.uk/sub
browsing to http://www.examplesproject.co.uk/sub doesn't duplicate the sub-domain by showing the content (for SEO purposes)
and also
Main domain examplesproject.co.uk is forced to use www - http://www.examplesproject.co.uk.
Sub domain sub.examplesproject.co.uk is force NOT to use www - http://sub.examplesproject.co.uk.
If anyone can help, I would be really grateful. By the way, locally I have set up virtual hosts http://examplesproject and http://sub.examplesproject using wamp and hosts file to replicate the online behaviour.
Thanks alot for reading. Answers/suggestions welcome.
Sorry about that Tim Post! I have put the content in this time! Nice one for looking at this. Hope that this helps someone.
This is the solution that worked for me (thanks to Jim (jdMorgan) at webmasterworld for this - http://www.webmasterworld.com/apache/4254301.htm)..
Put all of these rules, in this order, into the root .htaccess:
# Externally redirect direct client requests for test subdomain subdirectory paths to the test subdomain
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /sub/([^\ ]*)\ HTTP/
RewriteRule ^sub/(.*)$ http://sub.examplesproject\.co\.uk [R=301,L]
#
# Externally redirect all non-canonical, non-blank, non-test-subdomain hostname requests to canonical "www" main domain
RewriteCond %{HTTP_HOST} !^(www\.examplesproject\.co\.uk)?$
RewriteCond %{HTTP_HOST} !^([^.:]+\.)*sub\.([^.:]+\.)*examplesproject\.co\.uk [NC]
RewriteRule ^(.*)$ http://www.examplesproject.co.uk$1 [R=301,L]
#
# Externally redirect non-canonical subdomain hostname requests to canonical test subdomain
RewriteCond %{HTTP_HOST} ^([^.:]+\.)*sub\.([^.:]+\.)*examplesproject\.co\.uk [NC]
RewriteCond %{HTTP_HOST} !^sub\.examplesproject\.co\.uk$
RewriteRule ^(.*)$ http://sub.examplesproject.co.uk/$1 [R=301,L]
#
# Internally rewrite sub-domain requests to subdirectory path
RewriteCond %{HTTP_HOST} ^sub\.examplesproject\.co\.uk$
RewriteCond $1 !^sub/
RewriteRule ^(.*)$ /sub/$1 [L]
Checking THE_REQUEST in the now-first rule prevents the infinite redirection loop problem you encountered.
Note that exact hostnames are now enforced due to the very-careful use of case-sensitivity and anchoring.
Nice one aiit!

Resources