URL Rewrite create subdomain in cakephp
Asked Answered
K

1

9

I am using Cakephp framework 2.2.2.

I want to create a personalized URL for the user. For example, if a user enters username=pragnesh, then they can access my site like: http://pragnesh.mylocalhost.com, same as in blinksale.com


My URL: http://mylocalhost.com/test/users/front_home

I want to access it at: http://test.mylocalhost.com/users/front_home

My URL: http://mylocalhost.com/test/setting/pages
can be accessed at: http://test.mylocalhost.com/setting/pages

any URL: http://mylocalhost.com/test/xxxxx/xxxxx
can be accessed at : http://test.mylocalhost.com/xxxxx/xxxxx


OR


URL: http://mylocalhost.com/users/front_home?site=test

I want to access it at: http://test.mylocalhost.com/users/front_home

My URL: http://mylocalhost.com/setting/pages?site=test
can be accessed at: http://test.mylocalhost.com/setting/pages

any URL: http://mylocalhost.com/xxxxx/xxxxx?site=test
can be accessed at: http://test.mylocalhost.com/xxxxx/xxxxx


My question may be possible duplicate of My cakephp 2.2 site not working on subdomain but there is no answer posted.

I have tried below code in \app\webroot.htaccess

htaccess subdomai (part 2)

RewriteCond %{QUERY_STRING} !^([^&]*&)*site=[^&]+
RewriteCond %{HTTP_HOST} !^www\.mylocalhost.com
RewriteCond %{HTTP_HOST} ([a-zA-Z0-9]+)\.mylocalhost.com
RewriteRule ^([^\.]*).php$ /$1.php?user=%1 [QSA,L,R]

URL rewrite for Subdomain

# capture first part of host name
RewriteCond %{HTTP_HOST} ^([^.]+)\.mylocalhost\.com$ [NC]
# make sure site= query parameter isn't there
RewriteCond %{QUERY_STRING} !(^|&)site= [NC]
# rewrite to current URI?site=backererence #1 from host name
RewriteRule ^ %{REQUEST_URI}?site=%1 [L,QSA]

but both not working for me.

my root .htaccss file is

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

\app.htaccess

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

\app\webroot.htaccess

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

Virtual Host in httpd.conf

<VirtualHost *:80>  
   DocumentRoot "E:/xampp/htdocs/bsale"
   ServerName mylocalhost.com
   ServerAlias *.mylocalhost.com
   <Directory "C:/xampp/htdocs/bsale">
       Order allow,deny
       Allow from all
   </Directory>
</VirtualHost>

HOST FILE

127.0.0.1   mylocalhost.com
127.0.0.1   *.mylocalhost.com
Korella answered 6/10, 2014 at 7:59 Comment(16)
What is DocumentRoot of your test subdomain? Can you also post VirtualHost entry of test subdomain from Apache config?Locket
document root is mylocalhost, i don't have access to hosting filesKorella
DocumentRoot needs to be full filesystem path and you need to know exact DocumentRoot for both root domain and test sub domain.Locket
can I post httd.conf file for localhost where i have created alias/virtual host of mylocalhost.com?Korella
@anubhava, can I do same on local server with creating alias?Korella
Not using alias but using VirtualHost you can create test.localhost and localhost host entries on your local Apache config.Locket
<VirtualHost *:80> DocumentRoot "E:/xampp/htdocs/bsale" ServerName mylocalhost.com ServerAlias *.mylocalhost.com <Directory "C:/xampp/htdocs/bsale"> Order allow,deny Allow from all </Directory> </VirtualHost> added in host file C:\Windows\System32\drivers\etc 127.0.0.1 mylocalhost.com 127.0.0.1 *.mylocalhost.comKorella
Let us continue this discussion in chat.Korella
So E:/xampp/htdocs/bsale is common DocumentRoot for both main domain and test sub-domain?Locket
yes, E:/xampp/htdocs/bsale is common DocumentRootKorella
I have created replica of blinksale.com where they can create personalize url and access link like pragneshc.blinksale.com or test.blinksale.comKorella
ok so what URL is causing problem for you now or what rewrite isn't working for you from above rules?Locket
yes, it is not working with above examplesKorella
If you mean this: if user enter username=pragnesh then they can access my site like http://pragnesh.mylocalhost.com then I think you're mistaken as subdomain creation cannot happen on-the-fly from rewrite rules.Locket
ok, can you please show me how i can do thisKorella
See I am not a server admin but as per my knowledge subdomains are not registered/created on the fly. They need to be registered with a hosting provider first.Locket
G
1

You can leave your .htaccess as in the default cakephp/.htaccess cakephp/app/.htaccess cakephp/app/webroot/.htaccess

You will need the virtual host like now have pointing at your app.

What you really need to look at is the app/Config/routes.php

take a look at the line that says:

CakePlugin::routes();

So, before that moment you can do anything you need (with routes). Lets imagine a user johndoe has http://johndoe.yourapp.com/

Get the subdomain from the url, something like this:

array_shift((explode(".",'subdomain.my.com')));

using:

$_SERVER['HTTP_HOST']


Router::connect('/users/front_home', array('controller' => 'users', 'action' => 'front_home', $yourSubDomain));

Or just leave the router alone and detect the subdomain in your AppController, and use it anywhere in your app.

Gough answered 6/5, 2015 at 14:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.