URL重写-在cakephp中创建子域


URL Rewrite create subdomain in cakephp

我使用的是Cakephp框架2.2.2。

我想为用户创建一个个性化的URL。例如,如果用户输入username=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

任何URL:http://mylocalhost.com/test/xxxxx/xxxxx
可以访问:http://test.mylocalhost.com/xxxxx/xxxxx



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

我想访问它:http://test.mylocalhost.com/users/front_home

我的URL:http://mylocalhost.com/setting/pages?site=test
可以访问:http://test.mylocalhost.com/setting/pages

任何URL:http://mylocalhost.com/xxxxx/xxxxx?site=test
可以访问:http://test.mylocalhost.com/xxxxx/xxxxx


我的问题可能是重复我的cakehp2.2网站不在子域上工作但没有任何回复。

我在''app''webroot.htaccess中尝试了以下代码

htaccess子域名(第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重写

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

但两者都不适合我。

我的根.htacss文件是

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

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

您可以将.htaccess保留为默认值cakehp/.htaccesscakehp/app/.htaccesscakehp/app/webroot/.htaccess

你将需要像现在一样指向你的应用程序的虚拟主机。

您真正需要查看的是app/Config/routes.php

看看上面写着:

CakePlugin::routes();

所以,在那之前,你可以做任何你需要的事情(通过路线)。让我们想象一个用户johndoehttp://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中检测子域,并在应用程序中的任何位置使用它。