yii2 urlManager + .htaccess = subdomain how to? - .htaccess

here's the challenge: i need to make the imitation of subdomains. Actually all the "subdomains" in the project are simple actions of the main SiteController like
example.com/index.php/site/subsite?id=subname
example.com/index.php/site/about?id=subname
example.com/index.php/site/contacts?id=subname
etc. I need them to look like
subname.example.com/
subname.example.com/about
subname.example.com/contacts
etc. I have included urlManager rules like this:
'rules' => [
'http://<id:\w+>.site.com/about' => 'site/about',
'http://<id:\w+>.site.com/contacts' => 'site/contacts',
'http://<id:\w+>.site.com' => 'site/subsite',
],
And made a .htaccess file that is :
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTP_HOST} ^([a-zA-Z0-9]*).site.com [NC]
RewriteRule %{HTTP_HOST} "^([a-zA-Z0-9]*)$.site.com" "site.com/index.php/subsite?id=$1" [L]
But it (predictably) doesnt work as i am not strong in rewrite rules. What am i doing wrong ?

I would do it like this:
Configure one virtual host on web server to host all subdomain and point to the Yii2 application
Write component Subdomain.php and put it in frontend\components
<?php
namespace frontend\components;
use Yii;
use yii\base\Component;
class Subdomain extends Component {
private $_subdomain = false;
public function init() {
parent::init();
list($this->_subdomain) = explode('.', $_SERVER['HTTP_HOST']);
}
public function __toString() {
return $this->_subdomain;
}
}
?>
In frontend\config\main.php in the 'components' section add:
'subdomain' => [
'class' => 'frontend\components\Subdomain'
],
Then in the project where I need subdomain I would use:
$subdomain = \Yii::$app->subdomain;

Related

My .htaccess works on the my main login page but subsequent redirects does not work

Hi i am new to codeignitor my application works fine in my local host but when i bring it live server it gives me the below error
Error after login
My config file i have already set the base_url to
$config['base_url'] = 'http://diamondglass.com.sg//portal/';
and
$config['index_page'] = '';
My .htaccess is in my portal folder
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 index.php
</IfModule>
My index function in my project Controller
function index()
{
$data['projects'] = $this->project_model->getAll();
$data['users'] = $this->user_model->getAll();
// set array of items in session for page
$arraydata = array(
'mainsection' => 'Projects',
'subsection' => 'Projects'
);
$data['statuscount'] = $this->project_model->getStatusCount();
$this->session->set_userdata($arraydata);
$this->load->view('admin/project/index',$data);
//echo '<pre>';
//print_r($this->session->userdata());
}
My Login Controller
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Login extends CI_Controller {
function __construct()
{
parent::__construct();
if($this->session->userdata('user'))
redirect('admin/project');
$this->load->helper(array('form'));
$this->load->library(array('form_validation'));
}
public function index()
{
$this->load->view('login');
}
//To Verify Logins
function verify()
{
$this->load->model('user_model');
/* Load form validation library */
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username', 'required');
$this->form_validation->set_rules('password', 'Password', 'required');
if($this->form_validation->run() == TRUE)
{
//model function
$this->load->model('user_model');
$check = $this->user_model->validate();
if($check)
{
$this->session->set_userdata('user','1');
$this->session->set_userdata('currentUser',$check);
redirect('admin/project');
}
else
{
$this->session->set_flashdata('error', 'Invalid Username and Password');
redirect('login');
}
}
else
{
//false
redirect('login');
}
}
}
I have been searching the web high and low for a solution, but i can figure this out. I know it got to do with .htaccess but can't figure this out. can someone help
IMHO, your base_url has wrong (// after .sg)
Second, make sure your view-file is located at: ../Views/admin/project/index.php
You don't have to put the IfModule tags every time. Use this code:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
ErrorDocument 404 http://diamondglass.com.sg/404.html
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/404/$
RewriteRule ^(.*)$ <YourRelativePathToPHPFile>/404.html [L]
Make sure you have a custom 404 page

Url passed on has Escaped Characters. Htaccess rule to correct

I need advise for htaccess rule to decode url string for instance:
Actual Page is
http://www.mycarhelpline.com/index.php?option=com_latestnews&view=detail&n_id=953&Itemid=10
But Page is cached in Google as
http://www.mycarhelpline.com/index.php%3Foption%3Dcom_latestnews%26view%3Ddetail%26n_id%3D953%26Itemid%3D10
Decoded string will lead to correct url
I am sorry as is not at all aware of htaccess rule, hence not pasting code. Is there any htaccess rule to remove escaped characters and lead to correct url through redirect
Update
Like as given in
https://docs.joomla.org/J2.5:Developing_a_MVC_Component/Basic_backend
<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
// import joomla controller library
jimport('joomla.application.component.controller');
// Get an instance of the controller prefixed by HelloWorld
$controller = JController::getInstance('HelloWorld');
// Get the task
$jinput = JFactory::getApplication()->input;
$task = $jinput->get('task', "", 'STR' );
// Perform the Request task
$controller->execute($task);
// Redirect if set by the controller
$controller->redirect();
Shall i add this line in get the task - will it do the needful
$url = urldecode($jinput->get('url', '', 'string'));
Final Code is
<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
require_once( JPATH_COMPONENT.DS.'pager.cls.php' );
// import joomla controller library
jimport('joomla.application.component.controller');
// Get an instance of the controller prefixed by HelloWorld
$controller = JController::getInstance('HelloWorld');
// Get the task
$jinput = JFactory::getApplication()->input;
$task = urldecode($jinput->get('task', "", 'STR' ));
// Perform the Request task
$controller->execute($task);
// Redirect if set by the controller
$controller->redirect();
?>
HTACCESS COde - Though working Good Partially but is notconverting %3f to ?
RewriteCond %{QUERY_STRING} ^(.*)\?(.*)$
RewriteRule ^(.*)$ /$1?%1\%3F%2 [L]
RewriteCond %{QUERY_STRING} !^$
RewriteRule ^(.*)$ /$1\%3F%{QUERY_STRING}? [L,NE]
Update - Final HTACCESS
RewriteCond %{QUERY_STRING} !^$
RewriteRule ^(.*)$ /$1?%{QUERY_STRING} [L,NE]

Controller & routes

According to the tutorial, it is possible to handle all the requests for static pages through application/controllers/pages.php
class Pages extends CI_Controller {
public function view($page = 'home')
{
if( ! file_exists('application/views/pages/'.$page.'.php'))
{
// Whoops, we don't have a page for that!
show_404();
}
$data['title'] = ucfirst($page); // Capitalize the first letter
$this->load->view('templates/header', $data);
$this->load->view('templates/nav', $data);
$this->load->view('pages/'.$page, $data);
$this->load->view('templates/aside_right', $data);
$this->load->view('templates/bottom', $data);
}
}
This works for the "home"-page, but I don't seem to be able to call views/pages/about for example.
I tried to make a seperate controller for the about-page. It works, but it feels somewhat wrong.
application/controllers/about.php
class About extends CI_Controller {
public function index()
{
$this->load->view('templates/header');
$this->load->view('templates/nav');
$this->load->view('pages/about');
$this->load->view('templates/aside_right');
$this->load->view('templates/bottom');
}
}
I also have issues with my htaccess file or routes file. With the About-controller written above, I can only get to the page by typing domain.com/index.php/about. I'd like it to be domain.com/about etc.
This is what my routes.php's like:
$route['about'] = 'about';
$route['(:any)'] = 'pages/view/$1';
$route['default_controller'] = 'pages/view';
My Htaccess:
RewriteEngine on
RewriteBase /
# Hide the application and system directories by redirecting the request to index.php
RewriteRule ^(application|system|\.svn) index.php/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [QSA,L]
As you already said, you don't need to use another controller for the about pages. Your problem is with your routes.php.
$route['about'] = 'about';
$route['(:any)'] = 'pages/view/$1';
$route['default_controller'] = 'pages/view';
In this way, it will search for a controller named "about" and it doesn't find the controller. If you erase the first line:
$route['(:any)'] = 'pages/view/$1';
$route['default_controller'] = 'pages/view';
It should work. In this case, as you request any page, for example about it will call pages/view/about, where pages is the controller, view is the function and about is the argument that is passed to the function (replacing the default $page = home).
I also spotted another error in your logic. You wrote
This works for the "home"-page, but I don't seem to be able to call views/pages/about for example.
You don't have to call view/pages/about. You've to call pages/view/about. Remember that the syntax is always the same Controller/Function/Variable1/Variable2/Variable3. So, you should be able to see the about page with http://yourdomain.com/index.php/pages/view/about or only with http://yourdomain.com/about if you have the $route['(:any)'] = pages/view/$1 rules in your routes.php.

500 error after configuration page, setting up magento with substore

I have created a working magento site now I am trying to create a test environment based on the existing working site. It is on the same server and will be under a different sub domain.
workingSubDomain.domain.com/store/
newSubDomain.domain.com/store/
I have kept the substore directory structure the same as the working site.
I have gone through and set all of the folders to 755 with the app/etc/, media, var set to 777
After I click continue on the installation wizard configuration page I recieve a 500 error.
The server log file shows the following:
PHP Fatal error: Call to a member function insert() on a non-object in /var/www/vhosts/domain.com/magentoFolder/subStore/app/code/core/Mage/Core/Model/Resource/Resource.php on line 133, referer: http://newSubDomain.domain.com/subStore/index.php/install/wizard/config/?config%5Blocale%5D=en_US&config%5Btimezone%5D=America%2FChicago&config%5Bcurrency%5D=USD
I have tried the following url rewrites in the .htaccess folder:
RewriteEngine On
RewriteBase /clint/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /clint/index.php [L]
and
RewriteBase /magentoFolder/subStore
and
RewriteBase /magentoFolder/subStore/
and
RewriteBase /magentoFolder/
and
RewriteBase /subStore/
I have cleared out the var folder as well.
Any recommendations would be much appreciated.
Here is the code from line 133 in the resource.php file:
* Set module version into DB
*
* #param string $resName
* #param string $version
* #return int
*/
public function setDbVersion($resName, $version)
{
$dbModuleInfo = array(
'code' => $resName,
'version' => $version,
);
if ($this->getDbVersion($resName)) {
self::$_versions[$resName] = $version;
return $this->_getWriteAdapter()->update($this->getMainTable(),
$dbModuleInfo,
array('code = ?' => $resName));
} else {
self::$_versions[$resName] = $version;
return $this->_getWriteAdapter()->insert($this->getMainTable(), $dbModuleInfo);
}
}
It is not an apache error, it's a php error. Take a look at /var/www/vhosts/domain.com/magentoFolder/subStore/app/code/core/Mage/Core/Model/Resource/Resource.php, line 133
Maybe it's a database connection error.

Codeigniter, get variable from segment-based URL with querystrings

i've removed the index.php from my url but now i need to get some variable from the url , the CI solution is update the config.php
$config['uri_protocol'] = "PATH_INFO";
$config['enable_query_strings'] = TRUE;
this work perfectly when i use the index.php in the url , but i need it without index.php
this is my httacess
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA
any solution ? , thx
I had the same problem in CI, so I wrote this function to get the query strings from the $_SERVER['REQUEST_URI'] variable and extract them.
function extract_querystrings() {
$uri = $_SERVER['REQUEST_URI'];
$uri = explode('?',$uri);
if (count($uri)>1) {
foreach($uri AS $ur) {
$ur = explode('&',$ur);
foreach($ur AS $u) {
$u = explode('=',$u);
if ($u[0]=='/') continue;
$this->query_strings[$u[0]] = $this->input->xss_clean($u[1]);
}
}
}
//echo '<pre>',print_r($this->query_strings,true),'</pre>';
}
This function is called at my custom main controller's __construct().
You can modify the following line from
$this->query_strings[$u[0]] = $this->input->xss_clean($u[1]);
To
$_GET[$u[0]] = $this->input->xss_clean($u[1]);
And see if it works for you.
Have you looked at Elliot Haughin's Facebook library for CodeIgniter? It might provide some insights into using CI with Facebook.
http://www.haughin.com/code/facebook/

Resources