URL重写在cakephp中创建子域

Pra*_*han 9 php .htaccess mod-rewrite cakephp cakephp-2.2

我正在使用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

任何URL: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

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


我的问题可能是我的cakephp 2.2网站没有在子域工作的重复, 但没有发布答案.

我在\ app\webroot.htaccess中尝试过以下代码

htaccess subdomai(第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]
Run Code Online (Sandbox Code Playgroud)

子域的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]
Run Code Online (Sandbox Code Playgroud)

但两个都不适合我.

我的根.htaccss文件是

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

\ app.htaccess

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

\程序\ webroot.htaccess

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

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>
Run Code Online (Sandbox Code Playgroud)

主机文件

127.0.0.1   mylocalhost.com
127.0.0.1   *.mylocalhost.com
Run Code Online (Sandbox Code Playgroud)

vis*_*lex 1

您可以将 .htaccess 保留为默认的 cakephp/.htaccess cakephp/app/.htaccess cakephp/app/webroot/.htaccess

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

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

看看下面一行:

CakePlugin::routes();
Run Code Online (Sandbox Code Playgroud)

因此,在那一刻之前您可以做任何您需要的事情(通过路线)。让我们想象一下用户 johndoe 有http://johndoe.yourapp.com/

从 url 中获取子域名,如下所示:

array_shift((explode(".",'subdomain.my.com')));
Run Code Online (Sandbox Code Playgroud)

使用:

$_SERVER['HTTP_HOST']


Router::connect('/users/front_home', array('controller' => 'users', 'action' => 'front_home', $yourSubDomain));
Run Code Online (Sandbox Code Playgroud)

或者只保留路由器并检测 AppController 中的子域,然后在应用程序中的任何位置使用它。