URL重写在cakephp中创建子域 [英] URL Rewrite create subdomain in cakephp
问题描述
我使用 Cakephp 框架2.2.2。
我想为用户创建一个个性化的URL。例如,如果用户输入 username = 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=test
可通过以下网址访问: http://test.mylocalhost.com/setting/pages
任何网址: http://mylocalhost.com/xxxxx/xxxxx?site=test
可通过以下网址访问: http://test.mylocalhost.com/ xxxxx / xxxxx
我的问题可能是我的cakephp 2.2网站不工作在子域
但没有
我已尝试下列 \app\webroot.htaccess b
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]
#捕获主机名的第一部分
RewriteCond%{HTTP_HOST} ^([^。] +)\.mylocalhost\.com $ [NC]
#确保站点=查询参数不存在
RewriteCond%{QUERY_STRING}!(^ |&)site = [NC]
#重写当前URI?site =主机名的backererence#1
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>
\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>
DocumentRootE:/ xampp / htdocs / bsale
ServerName mylocalhost.com
ServerAlias * .mylocalhost.com
< DirectoryC:/ xampp / htdocs / bsale >
订单允许,拒绝
允许所有
< / Directory>
< / VirtualHost>
HOST FILE
127.0.0.1 mylocalhost.com
127.0.0.1 * .mylocalhost.com
您可以将.htaccess保留为默认值
cakephp / .htaccess
cakephp / app / .htaccess
cakephp / app / webroot / .htaccess
您现在需要虚拟主机指向您的应用程式。
你真正需要看的是应用程序/ Config / routes.php
该行说:
CakePlugin :: routes
所以,在那之前,你可以做任何你需要的
让我们想像一个用户johndoe有 http://johndoe.yourapp.com/
从网址获取子域,如下所示:
array_shift (。,'subdomain.my.com')));
使用:
code> $ _ SERVER ['HTTP_HOST']
Router :: connect('/ users / front_home',array('controller'=>'users','action '=>'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 subdomain but there is no answer posted.
I have tried below code in \app\webroot.htaccess
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]
# 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 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.
这篇关于URL重写在cakephp中创建子域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!