2014-10-06 121 views
9

我正在使用Cakephp框架2.2.2。URL重寫在cakephp中創建子域

我想爲用戶創建一個個性化的URL。例如,如果用戶輸入用戶名= pragnesh, 那麼他們可以訪問我的網站,如:http://pragnesh.mylocalhost.com,相同blinksale.com


我的URL:http://mylocalhost.com/test/users/front_home

我想http://test.mylocalhost.com/users/front_home

我的URL:http://mylocalhost.com/test/setting/pages在訪問http://test.mylocalhost.com/setting/pages

任何網址: 可以訪問http://test.mylocalhost.com/xxxxx/xxxxx


OR


http://mylocalhost.com/test/xxxxx/xxxxx
可以在訪問

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

我想在訪問它:http://test.mylocalhost.com/users/front_home

我的URL:http://test.mylocalhost.com/setting/pages

任何網址:http://mylocalhost.com/setting/pages?site=test
可以訪問http://mylocalhost.com/xxxxx/xxxxx?site=test
可以訪問:http://test.mylocalhost.com/xxxxx/xxxxx


我的問題可能是My cakephp 2.2 site not working on subdomain 可能重複,但沒有答案公佈。

我在嘗試下面的代碼\程序\ 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] 

但兩者不是爲我工作。

我的根。htaccss文件

<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> 

\程序\ webroot.htaccess

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

在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> 

主機文件

127.0.0.1 mylocalhost.com 
127.0.0.1 *.mylocalhost.com 
+0

什麼是您的測試子域的DocumentRoot?你是否也可以從Apache配置文件中發佈'test'子域的'VirtualHost'條目? – anubhava 2014-10-13 06:43:16

+0

文檔根目錄是mylocalhost,我無法訪問主機文件 – 2014-10-13 06:59:50

+0

'DocumentRoot'需要是完整的文件系統路徑,並且您需要知道確切的'DocumentRoot'用於根域和'test'子域。 – anubhava 2014-10-13 07:01:24

回答

1

您可以在默認留下您的.htaccess爲 的CakePHP /的.htaccess 的CakePHP /應用/的.htaccess 的CakePHP /應用程序/ Web根目錄/的.htaccess

您需要像現在這樣的虛擬主機已經指向你的應用程序。

你真正需要看的是應用程序/配置/ routes.php文件

看看這行:

CakePlugin::routes(); 

所以,在那之前,你可以做任何你需要(與路線)。 讓我們設想一個用戶johndoe的具有http://johndoe.yourapp.com/

從URL獲取子域,像這樣:

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

使用:

$_SERVER['HTTP_HOST'] 


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

或只保留路由器單獨和檢測子域你的AppController,並在你的應用中的任何地方使用它。