88

Is it possible to use .htaccess to rewrite a sub domain to a directory?

Example:

  • http://sub.domain.example/

shows the content of

  • http://domain.example/subdomains/sub/
Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
Erik Djupvik
  • 1,187
  • 1
  • 10
  • 13

9 Answers9

117

Try putting this in your .htaccess file:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^sub.domain.example
RewriteRule ^(.*)$ /subdomains/sub/$1 [L,NC,QSA]

For a more general rule (that works with any subdomain, not just sub) replace the last two lines with this:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.example
RewriteRule ^(.*)$ subdomains/%1/$1 [L,NC,QSA]
Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
Ansari
  • 8,168
  • 2
  • 23
  • 34
  • 3
    Ansari's solution will work, however, it when I navigate to `http://test.domain.com`, the URL redirects me to `http://test.domain.com/subdomains/test/`. Is it possible without showing the `/subdomains/test/` at the end? – Erik Djupvik May 17 '12 at 20:26
  • You mean it redirects to `http://domain.com/subdomains/test`? And you want the redirection to be invisible? – Ansari May 17 '12 at 20:37
  • 2
    In that case, I believe you will have to look into using [mod_proxy](http://httpd.apache.org/docs/2.2/mod/mod_proxy.html) and specify a `P` flag in addition to the `L`. – Ansari May 17 '12 at 20:39
  • 2
    No, it redirects to `http://test.domain.com/subdomains/test/`, when I only want it to show `http://test.domain.com/` – Erik Djupvik May 17 '12 at 20:53
  • 1
    I can't see how it redirects to `test.domain.com/subdomains/test` - maybe there is something else going on here. Can you add `RewriteBase /` right after `RewriteEngine on`? Also, if you want to keep it showing `test.domain.com` you will have to use mod_proxy and the `[P]` flag. – Ansari May 17 '12 at 20:59
  • How do I implement the mod_proxy? Also, adding `RewriteBase /` breaks the script and makes it display the index page of the root folder. – Erik Djupvik May 17 '12 at 21:15
  • I need a portable solution without explicit domain name. – Tomáš Zato Sep 15 '14 at 22:11
  • @TomášZato make a new question :) – Ansari Sep 15 '14 at 23:12
  • See Lee Kowalkowski's answer above if you want to keep just the subdomain visible and not display the whole path in the browser. – Bangkokian Jan 26 '16 at 22:36
  • 1
    I was able to get it to work using @anubhava's answer here: http://stackoverflow.com/a/22395008/128984 – Sir CodesALot Feb 23 '16 at 19:07
  • It was redirecting to `*/subdomains/test` because of this `RewriteRule ^(.*)$ http://domain.com/subdomains/%1/$1 [L,NC,QSA]`. You could replace it by `RewriteRule ^(.*)$ http://domain.com/$1 [L,NC,QSA]` – Scalpweb Apr 14 '16 at 21:31
  • It could be a version error, perhaps, but ditto @anubhava's answer (see Micah's comment above). Worked for me when this answer did not. – Parapluie Sep 06 '17 at 14:01
  • can i mask the url in this way, i need this answer, but it redirects to the folder path, i want to preserve the subdomain url in address bar, any way ???? – ReNiSh AR May 29 '18 at 14:59
  • It redirects because the RewriteRule target contains "http://". You need to remove the domain. This means `http://example.com/subdomains/%1/$1` should be `subdomains/%1/$1`. Look here: https://stackoverflow.com/a/10165787/318765 – mgutt Sep 05 '19 at 14:34
53

I'm not a mod_rewrite expert and often struggle with it, but I have done this on one of my sites. It might need other flags, etc., depending on your circumstances. I'm using this:

RewriteEngine on

RewriteCond %{HTTP_HOST} ^subdomain\.example\.com$
RewriteCond %{REQUEST_URI} !^/subdomains/subdomain
RewriteRule ^(.*)$ /subdomains/subdomain/$1 [L]

Any other rewrite rules for the rest of the site must go afterwards to prevent them from interfering with your subdomain rewrites.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Lee Kowalkowski
  • 11,591
  • 3
  • 40
  • 46
  • I would say so. I found this setting is the right one as the same as my answer [here](http://stackoverflow.com/a/29363720/4058484). – eQ19 Mar 31 '15 at 09:13
  • 5
    Just as a reference, in case someone beginning with `.htaccess` comes to this page (like I did), besides creating the actual `.htaccess` file, for it to become effective, you need to set `AllowOverride` in your `VirtualHost` settings (http://stackoverflow.com/a/22819550/1657502), enable Apache's `rewrite` module and then restart Apache (http://stackoverflow.com/a/5758551/1657502) – Antônio Medeiros Apr 20 '16 at 23:37
  • This wont work if you use www to visit subdomain. @Minh solution will work in all cases. – Rohan Khude Jan 18 '19 at 09:46
  • @RohanKhude, do you mean if you want www.example.com and example.com to serve different content? Because usually, for SEO, you would have www.example.com to issue a permanent redirect to example.com or vice versa. – Lee Kowalkowski Jan 18 '19 at 09:57
  • No, I just meant to say above htaccess snippet is not working when i visit www.subdomain.example.com. – Rohan Khude Jan 18 '19 at 10:02
  • 1
    @RohanKhude www as a subdomain of a subdomain is also something you'd also have to get to work at the DNS level, isn't it? I think your expectation that it should work for any arbitrary subdomain is incorrect. Minh's answer (https://stackoverflow.com/a/41389905/30945) assumes your .htaccess configuration supports 1 and only 1 subdomain, so is less robust in that aspect. My solution supports as many subdomains as you need, but yes, you need to explicitly define every supported subdomain, but an optional `www.` prefix is probably best supported via a redirect than a rewrite. – Lee Kowalkowski Jan 18 '19 at 10:12
20

You can use the following rule in .htaccess to rewrite a subdomain to a subfolder:

RewriteEngine On

 # If the host is "sub.domain.example"
 RewriteCond %{HTTP_HOST} ^sub.domain.example$ [NC]
 # Then rewrite any request to /folder
 RewriteRule ^((?!folder).*)$ /folder/$1 [NC,L]

Line-by-line explanation:

  RewriteEngine on

The line above tells the server to turn on the engine for rewriting URLs.

  RewriteCond %{HTTP_HOST} ^sub.domain.example$ [NC]

This line is a condition for the RewriteRule where we match against the HTTP host using a regex pattern. The condition says that if the host is sub.domain.example then execute the rule.

 RewriteRule ^((?!folder).*)$ /folder/$1 [NC,L]

The rule matches http://sub.domain.example/foo and internally redirects it to http://sub.domain.example/folder/foo.

Replace sub.domain.example with your subdomain and folder with name of the folder you want to point your subdomain to.

Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
Amit Verma
  • 40,709
  • 21
  • 93
  • 115
  • 1
    This is the correct answer for me - explanation of how I got here as follows. Arrived here looking for an age old problem with subdomain redirect that broke apparently when I installed SSL on the main site TLD. The redirect was initially still trying to use the main site TLD still and breaking security rules. What I needed was the keep the subdomain (that was on a completely different TLD) on the same host but away from the main site. Adding a rewrite rule as above enabled me to forward the complete subdomain to the folder hosted under the main TLD site. – bownie Jul 19 '17 at 18:33
8

I had the same problem, and found a detailed explanation in http://www.webmasterworld.com/apache/3163397.htm

My solution (the subdomains contents should be in a folder called sd_subdomain:

Options +FollowSymLinks -MultiViews

RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} subdomain\.domain\.example
RewriteCond $1 !^sd_
RewriteRule (.*) /sd_subdomain/$1 [L]
Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
Pablo Torrecilla
  • 2,098
  • 20
  • 15
4

This redirects to the same folder to a subdomain:

.httaccess

RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^([^\.]+)\.domain\.example$ [NC]
RewriteRule ^(.*)$ http://domain\.example/subdomains/%1
Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
jmsmarcelo
  • 95
  • 2
  • 11
3

Try to putting this .htaccess file in the subdomain folder:

RewriteEngine On

RewriteRule ^(.*)?$ ./subdomains/sub/$1

It redirects to http://example.org/subdomains/sub/, when you only want it to show http://sub.example.org/.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
2

Redirect subdomain directory:

RewriteCond %{HTTP_HOST} ^([^.]+)\.(archive\.example\.com)$ [NC]
RewriteRule ^ http://%2/%1%{REQUEST_URI} [L,R=301]
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mayank Awasthi
  • 491
  • 2
  • 9
-1

For any sub domain request, use this:

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.band\.s\.example
RewriteCond %{HTTP_HOST} ^(.*)\.band\.s\.example
RewriteCond %{REQUEST_URI} !^/([a-zA-Z0-9-z\-]+)
RewriteRule ^(.*)$ /%1/$1 [L]

Just make some folder same as sub domain name you need. Folder must be exist like this: domain.example/sub for sub.domain.example.

Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
Hafij Khan
  • 24
  • 2
-1

Edit file .htaccess:

RewriteEngine On 
RewriteCond %{HTTPS} off 
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Balaji
  • 9,657
  • 5
  • 47
  • 47
  • 1
    While this code may provide a solution to the question, it's better to add context as to why/how it works. This can help future users learn and eventually apply that knowledge to their own code. You are also likely to have positive-feedback/upvotes from users, when the code is explained. – Amit Verma Feb 17 '21 at 05:46
  • An explanation would be in order. – Peter Mortensen Aug 20 '21 at 23:55
  • This doesn't answer the question. This is an HTTP to HTTPS (same host) redirect, which has nothing to do with the question. – MrWhite May 30 '22 at 23:31