Yii Framework and HTML5 template file together - yii

I have one question:
my website would be localhost/carfinancing/index.php
I have different index.php page right now which is in HTML5 template and Yii is installed in "application" directory.
When I copy paste this directory then localhost/carfinancing/application/web/ shows my yii index page and localhost/carfinancing/application/web/online-car-loan-application shows another page.
Is there way for yii index file to show: localhost/carfinancing/application and for another page, to show: localhost/carfinancing/online-car-loan-application ?
I mean to remove the '/web/' word from the url?

It looks like you have 2 questions here. I will work on answering both.
1 Your HTML5 template should be a view file in the views/[controller name]
Since Yii2 is an MVC framework it is designed to have all requests come to a controller which then provides a view. If you are placing files in web to be viewed directly you are losing out on the benefits of MVC.
2 In order to remove the web/ directory from the URL, you will need to use the url manager.
Add the urlManager in the component of the config/web.php
$config = [
...
'components' => [
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
],
You will also want to setup your .htaccess as follows:
Options +FollowSymLinks
IndexIgnore */*
RewriteEngine on
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule . index.php
Then move everything out of the public_html directory, and put everything that is in the web directory back in public_html.
Reference this article if you are still having issues: http://fellowtuts.com/yii/hide-or-remove-basicweb-from-url-in-yii-2-0/

Related

htaccess maintenance redirect, but exclude some folders (hostgator multsite plan)

I am building a virtual store with Magento, and I am using the HostGator hosting with the "M" plan, that have support for more than one site, the problem is that Magento is in the root and I have another site inside the folder /example.com.br, but when I try to access this site via browser:
http://example.com.br
I am redirected to the Magento site:
http://magentosite.com.br/maintenance.html
The htaccess that are on root folder (htaccess of magento) is redirecting to the magento maintenance page http://magentosite.com.br/maintenance.html, and I need to stay only Magento in maintenance and not the sites that are within certain folders like folder /example.com.br, /example2.com.br and etc...
This is the maintenance code I'm using in Magento (that are in Root):
RewriteEngine On
RewriteBase /
# allow my ip to access magento store
RewriteCond %{REMOTE_ADDR} !^177\.18\.228\.58
RewriteCond %{REQUEST_URI} !^/maintenance\.html$
RewriteRule ^(.*)$ https://magentosite.com.br/maintenance.html [R=307,L]
I tryed this solution https://stackoverflow.com/a/6528448/2761794 but doesn't worked for me...
I used Magento's maintenance mode, and instead of using the "maintenance.flag" file, I created my custom file and redirected it, so I did not have to change the htaccess file, so the other sites in the subfolder worked normally.
I add this code to index.php of Magento root:
$maintenanceFile = 'maintenance.html';
$ip = $_SERVER['REMOTE_ADDR'];
$allowed = array('177.99.108.78', 'another ip...');
if (file_exists($maintenanceFile) && !in_array($ip, $allowed)) {
// include_once dirname(__FILE__) . '/errors/503.php';
include_once dirname(__FILE__) . '/maintenance.html';
exit;
}

apache configuration for react-router with browserHistory and baseName subdirectory

My application is in a subdirectory of the main web site. I have implemented basename and browserHistory and put the following recommended apache rewrite code into .htaccess in the app folder:
RewriteEngine On
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.html [L]
This recommended apache rewrite works to return browserHistory URLs to the application index.html. For some reason (see below), it does not find the "style.css" and "brundle.js" that are EMBEDDED in the index page HTML. The only thing that I can see that I have different in the example .htaccess file is that I have a RewriteBase value of "/react_librivox_search" because my application is in that subfolder of the site.
RewriteBase /react_librivox_search
I have tested using various different beginning and ending slashes for paths and files in .htaccess, and the problem is not that.
The problem seems to be that the react application is setting a GET value for the files that includes a part of the PATH variable that is supposedly only a part of the react-router definition:
<Route path="/book/:id" component={BookDataDisplay}/>
Note that the additional path segment "/book/" is being appended to the base URL, and when files are not found in THAT directory (which does not really exist), the server returns "index.html" for the missing files, which accounts for the mime-type error for the librivox_search.css file and the "<" error for the bundle.js file.
The stylesheet http://jstest.dd:8083/react_librivox_search/book/librivox_search.css was not loaded because its MIME type, “text/html”, is not “text/css”. librivox_search.css (embedded in index.html)
SyntaxError: expected expression, got '<' bundle.js (embedded in index.html)
The same unexpected addition of "book" to the URL is at work here as well. Neither of the embedded index.html files is in that subdirectory. But I want to maintain that "book" in the path, since it identifies what KIND of data is being passed to the route . . . which distinquishes it from other kinds of data. I just do not want to have it sent to the SERVER (but perhaps that cannot be avoided), since the actual index.html embedded files are not there.
I suppose rewriting "/react_librivox_search/book" as "/react_librivxo_search" might work, but it seems to be a hacky way to go about it. And I don't want to have to put duplicate bundle.js files in multiple directories (that works, but
what a maintenance nightmare THAT would be, and no doubt bad practice).
Or is it recommended to put a separate .htaccess in a REAL "book" subfolder that returns "librivox_search.css" and "bundle.js" (in the base directory) depending on the file request?

How to rewrite an URL in .htaccess to access a Slim-rendered page?

I'm trying to make a RewriteRule in .htaccess so that when users visit http://domain.com/pages/1 they actually get a page rendered by Slim framework accessible at http://domain.com/api/v1/pages/1.
The folder structure is as follows:
/api
-index.php <-- This is a Slim index file
-.htaccess <-- This is a Slim .htaccess (unmodified)
/components
-page.php <-- This is a page template that I use for rendering in Slim
index.php <-- This is a homepage
.htaccess <-- This is my wwwroot .htaccess
My page.php template looks like this:
<?php
?>
echo 'This is a page'
Here's the Slim part (for the sake of simplicity it doesn't yet pass the $id' variable to the template):
$app->group('/v1', function () use ($app) {
$app->get('/pages/:id', function ($id) use ($app) {
$app->render('../../components/page.php');
});
});
The rewrite rule in .htaccess under wwwroot is:
RewriteEngine On
RewriteRule ^pages/([0-9]*)?$ api/v1/pages/$1 [L]
There are two weird things that bother me:
In this configuration accessing the page at http://domain.com/api/v1/pages/1 works, but it doesn't work at http://domain.com/pages/1
When I try to access http://domain.com/pages/1 I get a 404 error, which is generated by Slim, rather than apache.
My question is as follows:
What is the correct/proper .htaccess rewrite rule to rewrite an URL for a page rendered by Slim in the above mentioned scenario?
This is due to improper rewrite pattern.
Try
RewriteEngine On
RewriteRule ^pages/([0-9]*)?$ api/v1/pages/$1

How manage several folders in my domain?

I want create subdomains like this:
domain.com/type/city
An examples:
domain.com/restaurants/new_york
domain.com/hotels/new_york
domain.com/restaurants/chicago
I have thousand of cities in a mysql database.
I thinked in some options:
Thousand of folders with an index.php for redirect (I think wrong way).
Create an sitemap with all links (domain.com?type=hotels&city=chicago) and manage they by code with the database.
Apache?
Please, which will be the best way for this? Thanks in advance!
You can solve this with a combination of PHP and Apache configuration. That is the most common solution and seen in popular PHP website software such as Drupal and Wordpress.
The idea is to let Apache send all traffic to one index.php file and pass the rest of the path as a parameter for PHP to handle with it.
You will need to be carefull with a few edgecases though; if file such as ./public/styles.css is requested, you don't want to serve that trough your PHP application but want apache to serve the file directly. Existing files will need to be handled by apache, all else by you application.
In your .htaccess:
# Rewrite URLs of the form 'x' to the form 'index.php?q=x'.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
The first line tells Apache to send normal files by itlself. Second line does the same for existing directories. Third line avoids that browsers (most notably version IE6) who request the example.com/favicon.ico don't hammer your PHP application.
Then it passes everything along to index.php and adds the rest of the path into the q param.
Inside index.php you can then read that, and take action with that:
<?php
$path = $_GET['q'];
$params = explode('/', $path);
print $path;
print_r($params);
?>
Thousands of folders would be the wrong way that is for sure.
If you start creating the sitemap with links of the type domain.com/?type=hotels&city=chicago you get a nice structure that you can manage programatically.
First get this started and working, then look up .htaccess and mod_rewrite which you can then use to map from domain.com/type/city to your links already functioning.
This seems both to be a good strategy for getting something working fast, and for ending up with the prettiest solution.

Using .htaccess mod_rewrite to bypass a deep directory structure to CSS assets

I have a setup like so
http://localhost/
http://localhost/ci_tada/
http://localhost/ci_tada/application
http://localhost/ci_tada/application/views
http://localhost/ci_tada/application/views/css
http://localhost/ci_tada/application/views/css/master.css
I dont want to have to write
http://localhost/ci_tada/application/views/css/
every time i wish to access a css file (the same will apply for images ect)
I want to be able to just use
http://localhost/ci_tada/css/master.css
and have it load the correct file.
The .htaccess file is located in the ci_tada folder.
Looks like you're using some sort of routes within a framework. You should check, because some frameworks give you the option to do it in the app configuration.
<IfModule mod_rewrite.c>
RewriteEngine On
# directory base, untogle if you want to
# rewrite only starting from /<directory/path>
# RewriteBase /
RewriteRule ^/css/(.*)$ index.php/ci_tada/application/views/css/$1 [PT,L]
#or RewriteRule ^/(.*)/css(.*)$ index.php/$0/views/css$1 [PT,L]
#or RewriteRule ^/(.*)/css/(.*)$ index.php/$0/views/css/$1 [PT,L]
</IfModule>
Beware that the last two redirect everything that contains /css/ in the path.
edit:1: It is considered best practice in CI (from what I've read), to set a static directory on your root like this:
/
.../static
......./css
......./js
.../application
......./controller
.../...
So that you can simply use /static/css/file.css inline in your views. Also see these resources if they can help:
http://codeigniter.com/forums/viewthread/60563/#297784
I feel you are talking about Zend Framework. Your few lines are
/
/application
How can every request can be redirected to CSS/? Then your application will not work!
In Zend Framework, application/layouts/scripts/layout.phtml, you mention css file like this:
<?php echo $this->headLink()->prependStylesheet($this->baseUrl().'/css/site.css'); ?>
Css file go in project folder. It is outside application folder:
\quickstart2\public\css.
Hope I will not receive -ve answer.