1

When a user goes to any subdomain of my website, for example test.domain.com or even sdjfhdihdfig.domain.com, I want the page of http://www.domain.com/directory to be shown (this directory will be the same regardless of the subdomain the user goes to).

If the user heads over to any page of any subdomain (for example, test.domain.com/apage.html), I want the page of http://www.domain.com/directory/[PAGE] to be shown (in this case the page www.domain.com/directory/apage.html would be shown, but of course this would change depending on what came after the subdomain).

I found something that seems to solve this problem, however, it redirects the user rather than keeping the subdomain as it is in the URL bar. If I went to test.domain.com/test, I want this URL to stay as it is, but the page located at www.domain.com/directory/test should be served instead.

Does anyone have any ideas about how I could do this with htaccess? Thanks in advance.

Alex Yorke
  • 48
  • 1
  • 8

2 Answers2

0

Try adding this to the .htaccess file in your web document root folder (often public_html or htdocs):

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?!www)[^.]+\.domain\.com [NC] 
RewriteRule ^(.*)$ http://www.domain.com/directory/$1 [R,L]

Once you are satisfied that the redirect works, you can change the R to R=301 to make it permanent. This assumes that mod_rewrite is both installed and activated for .htaccess files. If you are not sure, to check if mod_rewrite is even installed, look at the list of installed modules in the output of phpinfo(); By default, mod_rewrite is not enabled for .htaccess files. If you are managing your own server, open httpd.conf and make sure that the webroot directory block contains one of these lines: AllowOverride FileInfo or AllowOverride All

zx81
  • 41,100
  • 9
  • 89
  • 105
  • Thanks. This seems to be partly working but not completely. mod_rewrite is enabled, confirmed by AllowOverride All being set in /etc/apache2/sites-available/default. I've also tried enabling mod_rewrite using the command a2enmod rewrite which just tells me that mod_rewrite is already enabled. My website is hosted at https://www.quickdoge.com. If you go to any subdomain then you'll reach the websites homepage (not what I want) but if you go to any subdomain's page such as test.quickdoge.com/page, that'll redirect to the correct page. Even then, I don't want it to redirect. Any suggestions? – Alex Yorke Jul 23 '14 at 10:12
  • `Even then, I don't want it to redirect.` I thought the whole question was about redirecting. Not understanding. :) – zx81 Jul 23 '14 at 10:18
  • Sorry, it can be quite difficult to word things like this. I want the page of www.domain.com/directory/page to be shown to the user, but I want to keep the URL of the sub-domain in the user's address bar. At no point should the user know that every subdomain is the same content from the page at www.domain.com/directory/page - when someone goes to example.domain.com, it should look like they are still on example.domain.com, and if they go to sub.domain.com, it should look like they are still on sub.domain.com. Does this clarify it for you? – Alex Yorke Jul 23 '14 at 10:28
  • Yes, it does clarify it. In that case: 1. If the pages are on different servers, you have to change the `R` to `P` and it will only work if mod-proxy is enabled. If they are on the same server, then instead of doing an http redirect, we rewrite the page to the proper path, with just `L`, not `R` – zx81 Jul 23 '14 at 11:49
  • Thanks for all your help. I've figured out the answer to my problem but can't post it as an answer for another 8 hours (low reputation). While I didn't use the code in your answer, you've helped me to understand htaccess and some of the flags a bit better, so thanks for that :) – Alex Yorke Jul 23 '14 at 14:03
0

I managed to figure out the solution to my problem eventually.

This is my .htaccess document:

Options +FollowSymLinks
Options -MultiViews

RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?!www\.)?(.+)$
RewriteRule ^$ s/$1/ [L]

When someone goes to example.domain.com, the page at http://www.domain.com/s/ is displayed. When they go to a page like example.domain.com/s/page.html, the page at http://www.domain.com/s/page.html is displayed instead.

If the user goes to www.domain.com, this is not treated as a subdomain and instead my main website homepage is displayed.

I've set up a quick script so if someone decides to go to example.domain.com/page.html, instead of showing the page from www.domain.com/page.html, they are redirected to a 404 page on my server. The script (PHP) for how to do this is below:

if(substr($_SERVER['HTTP_HOST'], 0, 4) != 'www.' AND substr($_SERVER['REQUEST_URI'], 0, 3) != '/s/' AND $_SERVER['REQUEST_URI'] != '' AND $_SERVER['REQUEST_URI'] != '/') {
  header("Location: http://www.domain.com/404.php");
}

I got the htaccess file from htaccess get domain name from %{HTTP_HOST} into variable, minus the www and just adapted it a little bit.

I hope this helps someone in the future!

Community
  • 1
  • 1
Alex Yorke
  • 48
  • 1
  • 8