11

Have tried a lot of examples here in SO but none seem to work. Consider my set up:

/public_html/
     /app/
     /cgi-bin/
     /lib/
     /plugins/
     /revamp/
     /vendors/

I currently have a cakephp website site.com that has its files under /app/ folder, and the .htaccess I use right now looks like this:

<IfModule mod_rewrite.c>  
  RewriteEngine on

  ReWriteRule ^$ app/webroot/ [L]
  ReWriteRule (.*) app/webroot/$1 [L]
</IfModule>

I want to redirect the subdomain revamp.site.com to the folder /revamp/ which would be another cakephp site.

UPDATE 1:

Been asked to create a vhost and i did, it is set to the subdomain folder, the help I want is for a htaccess rule to work for both, the one above and also redirect to subdomain if the requested address has the subdomain on it before the domain...

UPDATE 2:

The following .htaccess gets me to my subdomain, but only to the index.html

<IfModule mod_rewrite.c>
   RewriteEngine on

    RewriteCond %{HTTP_HOST} ^site\.com$
    RewriteRule    (.*) app/webroot/$1 [L]

    RewriteCond %{HTTP_HOST} ^revamp\.site\.com$
    RewriteRule (.*) revamp/index.html [L]
</IfModule>

Its a mix of the two answers...though whatever path I put with subdomain, it only will get me to index.html (as it is hardcoded in the htaccess). Tried also revamp/$ and revamp/$1 with no luck.

UPDATE 3:

Tried this with edit of first answer:

<IfModule mod_rewrite.c>
   RewriteEngine on

   #subdomain simple site
   RewriteCond %{HTTP_HOST} ^subdomain\.test\.com$
   RewriteCond %{REQUEST_URI} !^/subdomain
   RewriteRule    ^(.*)$ subdomain/$1 [L]
   
   #cakephp site
   RewriteRule    ^$ app/webroot/    [L]
   RewriteRule    (.*) app/webroot/$1 [L]
</IfModule>

Still gives a 500, I also tried switching positions of both blocks

Update 4:

WORKING solution, having both cakephp sites under /public_html/app and /public_html/revamp/app

<IfModule mod_rewrite.c>
   RewriteEngine on

   RewriteCond %{HTTP_HOST} ^revamp\.site\.com$
   RewriteRule    ^(.*)$ revamp/app/webroot/$1 [NC,L]

   RewriteRule    ^$ app/webroot/    [L]
   RewriteRule    (.*) app/webroot/$1 [NC,L]
</IfModule>
Community
  • 1
  • 1
nullwriter
  • 785
  • 9
  • 34
  • are you using a shared hosting? – David Aguilar Oct 17 '15 at 01:31
  • Set up another VirtualHost for revamp and set DocumentRoot to be /revamp – user2182349 Oct 17 '15 at 01:39
  • @DavidAguilar I'm using a VPS yes – nullwriter Oct 20 '15 at 20:12
  • @user2182349 I keep getting told my the webhost to not change the root from vhost since it'll get reset everytime apache needs rebuild, they advise me for a htaccess extra rule to direct traffic looking for the subdomain – nullwriter Oct 20 '15 at 20:53
  • Wouldn't this work more easily by putting revamp into the app/webroot folder of the first one? Is there a reason not to do it that way? – Nick Zinger Oct 27 '15 at 09:22
  • @ChrisF any success? – sitilge Oct 30 '15 at 13:55
  • Because you are using shared host, i think best place to ask this question is their support. From my experience, procedure differs from provider to provider, and most of them have some UI option to do this.. – onedevteam.com Nov 02 '15 at 21:37
  • @sitilge sorry man, I have been really busy, I infact tried both the answers I got, which yielded no succesfull result =/ – nullwriter Nov 02 '15 at 22:07
  • @NickZinger because /app/ is under CakePHP framework, it will try to resolve it under the rules of the framework if I put /revamp/ under that folder – nullwriter Nov 02 '15 at 22:55
  • it seems RewriteRule ^$ app/webroot/ [L] is wrong, can you try it with some virtual host like RewriteCond %{HTTP_HOST} ^test\.com$ for cakephp site – Chetan Ameta Nov 04 '15 at 17:43
  • @ChetanAmeta the cakephp site loads perfectly, the one that gives me 500 is the subdomain – nullwriter Nov 04 '15 at 21:37

2 Answers2

4

You can achive required rewriting by using "Rewrite condition" for different different domain:

At my machine I cretaed a folder structure like

public_html
 -app
   -webroot
 -local
   -app
     -webroot

app, webroot and local directory in public_html folder and then app and webroot directory in local folder.

Now I created a virtual host entry as below:

<VirtualHost test.com:80>
 ServerName test.com
 ServerAlias local.test.com
 DocumentRoot /var/www/html/public_html

 ErrorLog ${APACHE_LOG_DIR}/error.log
 CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Means test.com and local.test.com both pointed at public_html directory.

Now your aim is, if domain is test.com then outer app should run and if domain is local.test.com then your inner(local folder) app should run.

As structure of .htaccess in cakephp i placed .htaccess file in both app and webroot folder of both application.

code of .htaccess in app/ directory

<IfModule mod_rewrite.c>
   RewriteEngine on
   RewriteRule    ^$    webroot/    [L]
   RewriteRule    (.*) webroot/$1    [L]
</IfModule>

code of .htaccess in webroot/

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>

now the changes in .htaccess of root directory i.e. public_html:

<IfModule mod_rewrite.c>
   RewriteEngine on
   #for test.com
   RewriteCond %{HTTP_HOST} ^test\.com$
   RewriteRule    ^(.*)$ app/webroot/$1 [NC,L]

   #for local.test.com
   RewriteCond %{HTTP_HOST} ^local\.test\.com$
   RewriteRule    ^(.*)$ local/app/webroot/$1 [NC,L]
</IfModule>
  1. RewriteCond %{HTTP_HOST} ^test\.com$ will identify that domain is test.com
  2. RewriteCond %{HTTP_HOST} ^local\.test\.com$ will identify that domain is local.test.com

For general sub domain rewrite you can use follow code:

Folder structure:

public_html
 -index.html
 -subdomain
  --index.html

Code of .htaccess

<IfModule mod_rewrite.c>
   RewriteEngine on

   #for subdomain.test.com
   RewriteCond %{HTTP_HOST} ^subdomain\.test\.com$
   RewriteCond %{REQUEST_URI} !^/subdomain
   RewriteRule    ^(.*)$ subdomain/$1 [L]
</IfModule>

now test.com will open index.html of public_html folder while subdomain.test.com will open index.html of subdomain folder

Update Answer 2

I created exact directory structure mentioned in question, and below htaccess code works for me:

<IfModule mod_rewrite.c>
   RewriteEngine on

   RewriteCond %{HTTP_HOST} ^test\.com$
   RewriteRule    ^(.*)$ app/webroot/$1 [NC,L]

  RewriteCond %{HTTP_HOST} ^revamp\.test\.com$
  RewriteCond %{REQUEST_URI} !^/revamp
  RewriteRule    ^(.*)$ revamp/$1 [NC,L]
</IfModule>
Chetan Ameta
  • 7,696
  • 3
  • 29
  • 44
2

Try

RewriteCond %{HTTP_HOST} ^revamp\.site\.com$
RewriteRule (.*) revamp.site.com/$1 [R=301,L]
RewriteRule ^$ revamp [L]

However, I would create a trivial new VirtualHost to handle requests to the subdomain

<VirtualHost *:80>
    ServerName revamp.site.com
    DocumentRoot /path/to/public_html/revamp
    <Directory>
        #your other rewrite rules here
    </Directory>
</VirtualHost>

References:

Community
  • 1
  • 1
sitilge
  • 3,687
  • 4
  • 30
  • 56