Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirecting subdomain to a folder inside main that contains a CakePHP app

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>
like image 684
nullwriter Avatar asked Oct 17 '15 01:10

nullwriter


Video Answer


2 Answers

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>
like image 126
Chetan Ameta Avatar answered Sep 19 '22 20:09

Chetan Ameta


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:

  • Make web root folder a sub-folder with .htaccess?
  • https://webmasters.stackexchange.com/questions/35726/how-to-redirect-requests-to-another-folder-using-htaccess
like image 40
sitilge Avatar answered Sep 17 '22 20:09

sitilge