本文介绍了URL重写创建子域在CakePHP中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的 CakePHP的框架2.2.2。

我想创建一个个性化的URL的用户。例如,如果用户输入的用户名= pragnesh 然后他们可以访问我的网站,如: http://pragnesh.mylocalhost.com ,相同blinksale.com


我的网址: http://mylocalhost.com/test/users/front_home

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

我的网址: http://mylocalhost.com/test/setting/pages
http://test.mylocalhost.com/setting/pages :可以访问

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



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

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

我的网址: http://mylocalhost.com/setting/pages?site=测试
http://test.mylocalhost.com/setting/pages :可以访问

任何网址: http://mylocalhost.com/xxxxx/xxxxx?site=测试
http://test.mylocalhost.com/xxxxx/xxxxx :可以访问


我的问题可能是我的CakePHP的2.2的网站不工作的子域但目前还没有答案公布。

我已经试过低于code \程序\ webroot.htaccess

htaccess的subdomai(部分2)

 的RewriteCond%{QUERY_STRING} ^([^&放大器;] *放大器;)*网站= [^&安培] +
的RewriteCond%{HTTP_HOST}!^ WWW \ .mylocalhost.com
的RewriteCond%{HTTP_HOST}([A-ZA-Z0-9] +)\。mylocalhost.com
重写规则^([^ \。] *)。PHP $ /$1.php?user=%1 [QSA,L,R]
 

URL重写的子域

 主机名#捕获第一部分
的RewriteCond%{HTTP_HOST} ^([^。] +)\。mylocalhost \ .COM $ [NC]
#确保网站=查询参数是不存在的
的RewriteCond%{QUERY_STRING}(^ |&安培)!网站= [NC]
#改写当前的URI?网站= backererence#1的主机名
重写规则^%{REQUEST_URI}?网站=%1 [L,QSA]
 

但两者不是为我工作。

我的根.htaccss文件

 < IfModule mod_rewrite.c>
  RewriteEngine叙述上
  重写规则^ $应用程序/ Web根目录/ [L]
  重写规则(。*)的应用程序/ Web根目录/ $ 1 [L]
< / IfModule>
 

\ app.htaccess

 < IfModule mod_rewrite.c>
    RewriteEngine叙述上
    重写规则^ $ Webroot公司/ [L]
    重写规则(。*)根目录/ $ 1 [L]
< / IfModule>
 

\程序\ webroot.htaccess

 < IfModule mod_rewrite.c>
    RewriteEngine叙述上
    的RewriteCond%{} REQUEST_FILENAME!-d
    的RewriteCond%{} REQUEST_FILENAME!-f
    重写规则^(。*)$的index.php [QSA,L]
< / IfModule>
 

在httpd.conf虚拟主机

 <虚拟主机*:80>
   的DocumentRootE:/ XAMPP / htdocs中/ bsale
   服务器名mylocalhost.com
   ServerAlias​​ * .mylocalhost.com
   <目录C:/ XAMPP / htdocs中/ bsale>
       订购允许,拒绝
       所有允许
   < /目录>
< /虚拟主机>
 

主机文件

  127.0.0.1 mylocalhost.com
127.0.0.1 * .mylocalhost.com
 

解决方案

您可以留下您的.htaccess作为默认CakePHP的/的.htaccessCakePHP的/应用/的.htaccessCakePHP的/应用程序/ Web根目录/的.htaccess

您需要的虚拟主机就像现在都指向您的应用程序。

你真正需要考虑的是应用程序/配置/ routes.php文件

看看就行,上面写着:

  CakePlugin ::航线();
 

所以,那一刻之前,你可以做任何你需要(有路由)。让我们设想一个用户JOHNDOE有 http://johndoe.yourapp.com/

从URL中获取的子域,是这样的:

  array_shift((爆炸(subdomain.my.com'))。);
 

使用:

  $ _ SERVER ['HTTP_HOST']


路由器::连接('/用户/ front_home',阵列('控制'=>'用户','行动'=>front_home',$ yourSubDomain));
 

或者刚刚离开路由器单独检测的子域名在你的AppController,并在任何地方使用它在你的应用程序。

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 subdomainbut 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
解决方案

You can leave your .htaccess as in the defaultcakephp/.htaccesscakephp/app/.htaccesscakephp/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.

这篇关于URL重写创建子域在CakePHP中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 13:42