How to create a sub-domain in CakePHP? - php

CakePHP version-2.5.5
My domain name is http://www.thechatfun.com
Profile page link - http://www.thechatfun.com/users/profile
Chat page link - http://www.thechatfun.com/chats/index
Above two link i want to looks like http://profile.thechatfun.com and http://www.chat.thechatfun.com
I am unable to make subdomain in the CakePHP.
Please help me
Thanks
ChatFun

As long as you can configure your domain records to point both chat and profile subdomains to your server then you can change the htaccess file in your webroot folder and add..
<IfModule mod_rewrite.c>
#standard cake htaccess stuff
...
RewriteCond %{HTTP_HOST} ^profile\.thechatfun\.com$ [NC]
RewriteRule ^(.*)$ http://www.thechatfun.com/users/profile/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^chat\.thechatfun\.com$ [NC]
RewriteRule ^(.*)$ http://www.thechatfun.com/chats/index/$1 [R=301,L]
</IfModule>
I have this exact requirement and this works for me.

Follow your context, inside this directory: /lib/Cake/Routing/Route , create file SubdomainRoute.php with content:
class SubdomainRoute extends CakeRoute {
public function match($params) {
$subdomain = isset($params['subdomain']) ? $params['subdomain'] : null;
unset($params['subdomain']);
$path = parent::match($params);
if ($subdomain) {
$path = 'http://' . $subdomain . '.thechatfun.com' . $path;
}
return $path;
}
}
When creating links you could do the following to make links pointing at other subdomains.
echo $this->Html->link(
'Profile',
array('subdomain' => 'profile', 'controller' => 'Users', 'action' => 'profile')
);
echo $this->Html->link(
'Chats',
array('subdomain' => 'chat', 'controller' => 'Chats', 'action' => 'index')
);
Reference: http://book.cakephp.org/2.0/en/appendices/new-features-in-cakephp-2-0.html#routes-can-return-full-urls

profile.thechatfun.com and www.chat.thechatfun.com are different domains. It's possible for a single http server to handle both of these domains, but it won't happen automatically.
Assuming your webserver is Apache, you will first need to configure the webserver to handle these domains correctly. You can add a VirtualHost directive so that both of these domains are handled by the same Virtual Host and share a document root, or you can add a Virtual Host for each and have separate document root directories for each domain.
Your webserver first receives the HTTP request, then passes the request to PHP for processing. So, if your webserver isn't configured properly to handle these domains, you won't have the ability to control this in PHP or CakePHP.

Related

Folder access overriding url routing in php

I am trying to write a PWA ready MVC framework in PHP. Controllers and Models are written in PHP, and the views are plain HTML assisted by AJAX.
So I started by building a URL routing class to manage requests. I also wrote a Session class, and it is working fine.
But my problem here is, I need some static HTML pages at the client side, for caching purposes. But I don’t want these files to be accessed without login. Let’s say I have a folder structure such as:
/ LIBS
/ route.php
/ Static
/ index.html
index.php
When I enter a URL like: mysite.com/foo/bar, routing class does the job and returns an array:
array (
0 => 'foo',
1 => 'bar',
)
That is good. But when I enter mysite.com/static it just goes into STATIC folder and brings index.html which is located inside that folder.
I want these files be accessed via php with Session control, after successful login. But I don’t want my visitors to access these files before logging in.
I tried to disable the access with htaccess, but this time I could not access them via PHP either.
Here is my index php:
<?php
include "/LIBS/route.php";
$route = new Route;
$route->init();
Here is my Router
<?php
class Route {
// Prints the URL array
public function init()
{
echo '<pre>' . var_export($this->_getUrl(), true) . '</pre>';
}
// gets the current URL and returns an array
private function _getUrl()
{
$url = isset($_GET['url']) ? $_GET['url'] : null;
$url = rtrim($url, '/');
$url = filter_var($url, FILTER_SANITIZE_URL);
$url = explode('/', $url);
return $url;
}
}
And here is my htaccess file
php_flag display_errors on
php_value error_reporting 9999
RewriteEngine On
RewriteBase /mvc/
# DirectoryIndex none.none
# Options -Indexes
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
There is a kind of relevant SO question here which doesn't seem to be the answer to my question.
I digged everywhere but could not find an answer. What am I missing?
Thanks

OpenCart Journal2 admin panel not loads

After changing from HTTP to HTTPS in my OpenCart installation with Journal2 theme/plugin I have problem - admin panel for Journal2 not loads, showing "Loading..." state and that's all.
Journal2 version = 2.13
OpenCart version = 2.3.0.2
Current stack: PHP 7 + Apache2 + MySQL
Also adding screenshot.
What is broken? I have updated website url from http:// to https:// in admin panel, also updated variables in
config.php
and
/admin/config.php
files. Maybe problems with caching?
Kindly check the two config files for correct configuration of Data Base and file folders.
Also u need to check the file in system/library/url.php
on that,
public function link($route, $args = '', $secure = false) {
if ($this->ssl && $secure) {
$url = 'https://' . $_SERVER['HTTP_HOST'] . rtrim(dirname($_SERVER['SCRIPT_NAME']), '/.\\') . '/index.php?route=' . $route;
} else {
$url = 'http://' . $_SERVER['HTTP_HOST'] . rtrim(dirname($_SERVER['SCRIPT_NAME']), '/.\\') . '/index.php?route=' . $route;
}
if ($args) {
if (is_array($args)) {
$url .= '&' . http_build_query($args);
} else {
$url .= str_replace('&', '&', '&' . ltrim($args, '&'));
}
}
foreach ($this->rewrite as $rewrite) {
$url = $rewrite->rewrite($url);
}
return $url;
}
Hope It will helps you.
Did you properly configure your site for SSL use? Let's go over some things here:
Not to sound repetitive, but are you very sure that both your config files are correct? Not sure what you meant before when you said you took "the path for the root" from some page when it's pretty much the url that anyone's going to go visit your site/store. I'm assuming your store/opencart is in it's own folder and not in the root directory, yes? If so:
admin/config.php file should have something like this:
// HTTP
define('HTTP_SERVER', 'http://www.yourwebsite.com/opencart/admin/');
define('HTTP_CATALOG', 'http://www.yourwebsite.com/opencart/');
// HTTPS
define('HTTPS_SERVER', 'https://www.yourwebsite.com/opencart/admin/');
define('HTTPS_CATALOG', 'https://www.yourwebsite.com/opencart/');
config.php file should have something like this:
// HTTP
define('HTTP_SERVER', 'http://www.yourwebsite.com/opencart/');
// HTTPS
define('HTTPS_SERVER', 'https://www.yourwebsite.com/opencart/');
and by what I recall from past experience that forward slash matters at the end.
Also, it matters very much if your url is: yourwebsite.com vs www.yourwebsite.com just to point out that, too.
Speaking of which... how about your .htaccess file? That could be a culprit here, too. I'm referring to the htaccess file for your root directory and not opencart's htaccess. That may need to be altered correctly to for https. As an example:
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.yourwebsite.com/$1 [R,L]
RewriteCond %{HTTP_HOST} ^(www.)?yourwebsite.com$
RewriteCond %{REQUEST_URI} !^/opencart/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /opencart/$1
RewriteCond %{HTTP_HOST} ^(www.)?yourwebsite.com$
RewriteRule ^(/)?$ opencart/index.php [L]
If you notice here, you need to include the server port and https. So maybe check that, too.

How to create Wildcard sub-domains

I am using joomla 3.3, I have a site on which user can create their own store with domain name so how can I create sub domain by which user can access their sub domain.
Suppose if a user create abc.example.com then that user should be able to access abs.example.com
So how to do this.
i have added these lines to .htaccess file
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.zzphonecase\.com(.*)$ /index.php/%2/%1
and these lines in index.php file of template
$host = explode ('.', $_SERVER ['HTTP_HOST']);
$subdomain = $host [0];
if((!strcasecmp ($subdomain, 'www')) || (!strcasecmp ($subdomain, 'zzphonecase')))
{
//main site enter code here
}
else
{
//enter code here
// subdomain
http_response_code (404);
echo 'Error 404 : your domain could not be found';
return false;
}
The problem is when and where you load the domain redirect. you're loading it at the template layer...way too late in the process. it has to be loaded before the joomla framework is loaded (top of root index.php. i use "virtual domains" for multisite domain management, and fabrik for custom forms & components extensively on my sites. you could create a form in fabrik that adds and entry & parameters to the virtual domain database table. little bit of a learning curve on how to execute it, but once you get it this will make it very simple for you. You can then setup your wildcard domain and point it to the site root and let the virtual domains component do all the work for you. Seriously...multisite management in joomla is a real pain. others have done the work for you!

Routing issues with Slim Framework running on AppFog

I've been successfully running Slim apps on a couple different servers and tried setting one up on AppFog today using the same structure, but it isn't running normally.
I'll start with my directory structure:
.htaccess
/public
.htaccess
index.php
/routes
/Slim
The root .htaccess file contains the DocumentRoot code from the AppFog docs.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^brs.aws.af.cm$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.brs.aws.af.cm$
RewriteCond %{REQUEST_URI} !public/
RewriteRule (.*) /public/$1 [L]
The /public directory is where my api code will go, and the Slim index.php and .htaccess files currently are. The index.php file contains two simple routes:
require '../Slim/Slim.php';
\Slim\Slim::registerAutoloader();
$app = new \Slim\Slim();
// Default GET route
$app->get('/', function () {
echo "Default GET route";
});
// Hello World route
$app->get('/hello/:name', function ($name) {
echo "Hello, $name";
});
$app->run();
The server is setup at http://brs.aws.af.cm/ and I've listed the main routes below:
/ => uses the default GET route
/hello/john => 404 Error
/public/hello/john => works, but requires "/public" in the url
And here's some extra weirdness. Seven-character routes result in a 404 error, six or less end up using the default GET route.
/123456 => shouldn't work, but uses the default GET route
/1234567 => 404 error
I'm completely stumped. I figure it has something to do with the DocumentRoot code, but I'm not sure what exactly. I've also tried setting
RewriteBase /public/
in /public/.htaccess but it doesn't seem to affect anything.
Any help would be greatly appreciated. Thanks!
There is a bug in the Slim PHP framework in Environment.php line 143. In particular, it assumes that the $_SERVER['SCRIPT_NAME'] path is compatible with $_SERVER['REQUEST_URI'] variable. In most cases this is probably true, however not when using MOD_REWRITE to hide an intermediate directory (as is happening in the .htaccess you quoted).
What's happening is $_SERVER['SCRIPT_NAME'] looks something like "/public/something..." but (because it is hidden), $_SERVER['REQUEST_URI'] looks like "/something...".
Slim is assuming the request URI is based on the script name, which is not the case here. I plan on notifying the Slim authors of the bug, but wanted to make note of it here as well.
You can fix/work around this by modifying Slim/Environment.php line 143 to this:
if (strpos($_SERVER['REQUEST_URI'], $_SERVER['SCRIPT_NAME']) === 0) {
$env['SCRIPT_NAME'] = $_SERVER['SCRIPT_NAME']; //Without URL rewrite
$env['PATH_INFO'] = substr_replace($_SERVER['REQUEST_URI'], '', 0, strlen($env['SCRIPT_NAME']));
} else {
$env['SCRIPT_NAME'] = str_replace('\\', '/', dirname($_SERVER['SCRIPT_NAME'])); //With URL rewrite
$env['PATH_INFO'] = $_SERVER['REQUEST_URI'];
}
// $env['PATH_INFO'] = substr_replace($_SERVER['REQUEST_URI'], '', 0, strlen($env['SCRIPT_NAME']));
At least that seems to work fine in my case. I believe the intention was to remove the path from the request uri, but that seems a pretty horrible way of doing it. If you need subdirectories to keep working you may need to do a bit more thinking. :)

how to change the url without changing the folder?

I am using MVC framework in my website.In my webpage URL file extension is not showing.I need to specify the file extension like
www.legacy.com/women/tops.php
but its showing only
www.legacy.com/women/tops/
for seo purpose I want to change the url with extension. But I can't change the folder or file name.
One more doubt I have that.My webpage is showing
www.legacy.com/women/tops/
like this I want to change this as www.legacy.com/women_tops.php/
Is it possible?
Thank you
Here is the code I use to associate a URL with a controller:
$arrCommands = array (
'home' => "contents",
'members' => "",
);
if ( $arrCommands[$command1] == "*" )
{
$includeFile = CONTROLLER_PATH . "/" . $command1 . "/" . $command2 . ".php";
if ( !file_exists($includeFile) )
$includeFile = CONTROLLER_PATH . "/" . $command1 . "/default.php";
}
elseif ( !array_key_exists($command1, $arrCommands) )
{
$includeFile = CONTROLLER_PATH . "/contents/" . $command1 . ".php";
if ( !file_exists($includeFile))
Header( "Location: http://metroplots.ragedev/404_error/" );
}
else
{
$includeFile = CONTROLLER_PATH . "/". $arrCommands[$command1] . "/" . $command1 . ".php";
if ( !file_exists($includeFile) )
$includeFile = CONTROLLER_PATH . "/contents/home.php";
}
include_once($includeFile);
.htaccess can do magic. Can rewrite anything to whatever you want, can get messy...
http://httpd.apache.org/docs/2.0/misc/rewriteguide.html
You should also be able to do this in the application itself. In most good frameworks you can use customize routes, here is zend example.
the technology your looking for is called url rewriting
the idea is taking a give url and rewriting it on the server side. lots of software use this method to not expose logic via get parameters...
e.g.
http://domain.com/blog/2
according to the server, this url is actually:
http://domain.com/index.php?cat=blog&page=2
on linux/apache servers is is achieved via modrewrite:
http://httpd.apache.org/docs/current/mod/mod_rewrite.html
using .htaccess files on the server that explain the rewrite rules and route urls.
microsoft iis servers have their own flavor (and syntax) called url rewrite:
http://www.iis.net/downloads/microsoft/url-rewrite
there are some tools out there to do this for you (here's an overview of 6 of them)
http://webm.ag/2009/12/15/6-of-the-best-mod-rewrite-generators/
but i feel like most times your best best is to manually create your own.
here's an example of the one i use on my site:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
basically this ruleset will redirect all traffic to index.php
in my MVC i have a generic controller called "url_logic" that runs first and looks at what the url is. and based upon it's logic it creates the necessary controllers to create the site (even if it's a 404 error controller).
hope that helps get you started!
also, if your using windows .htaccess files are tough to work with. i suggest naming them
htaccess.txt and when you upload them to the server rename them there.
You have to add below 2 line in htaccess
RewriteEngine On
RewriteRule ^women/tops /women_tops.php
create a index.php file, which will be read as default file for display and redirect it to your file.
What you are looking is mod_rewrite
mod_rewrite:
HIDING EXTENSION IN URL:
mod_rewrite Rewrite Rule generator will take a dynamic url given to it, and generate the correct syntax to place in a .htaccess file to allow the url to be rewritten in a spiderable format. The apache module mod_rewrite converts urls in a certain format to another format, and can be very useful in helping a site with dynamic content to be indexed.
NOTE : Before mod_rewrite put in action you should enable it in apache server.
Syntax: RewriteRule url-pattern url-new [[flag,...]]
Example: RewriteRule ^/foo/(.*)$ /bar/$1 [R,L]
Some useful links-
URL : http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html
URL : http://tomclegg.net/rewriterule
have a look at this web page. i think it may have answers for you.
Thank you for every one who replayed to this question.
$xepWords = explode('_',$command1);
if($xepWords[0] == 'women')
{
$includeFile = CONTROLLER_PATH .'/women/tops.php';
}
I added this code in controller action Now its working fine

Categories