Yii2 Pretty Url don't work in Share Hosting (Basic Template) - .htaccess

I m trying to deploy a small project build with Yii2 (Basic Template) in a Share Hosting enviroment. Everything is working fine in local.
The Host server was like below in the root directory:
/
---logs
---index.html
After reading the doc of Yii2. I then delete the file index.html and upload my project folders into the root directory. It s now looking like this:
/
---logs
---assets
---commands
---config
---controllers
--- ...
---views
---web
--- ...
After trying the website i got a blank page. I then decided to create a htaccess file in the root of host server to point the web folder like here
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteCond %{REQUEST_URI} !web/
RewriteRule (.*) /web/$1 [L]
After this i can get the index page very well with
www.example.com
and I get a blank page when i try to access for example (with url prettyUrl activated)
www.example.com/articles/list
But When I deactivate the prettyUrl, it s working like for example
www.example.com/web/index.php?r=articles/list
The .htaccess in the web folder is like this:
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
# Disable Directory Browsing
Options All -Indexes
and the url rules are set in the web.php file like below:
'urlManager' => [
'class' => 'yii\web\UrlManager',
'showScriptName' => false,
'enablePrettyUrl' => true,
'baseUrl' => '/',
'rules' => [
...
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
],
],

The main majority of the shared hosts disables some of the directives from the htaccess file to prevent some security issues. And if a restricted directive is found in your htaccess file you will get an 500 error page.
It's very likely that if you remove the Options +FollowSymLinks directive everything will work.
Additionally you can check out the apache error log for more information.

Related

Yii and .htaccess file on production server

I am new to yii and have some problem in configuring .htaccess file on production server.
On localhost :
Location of Application : /www/connect_donors/
Default URL that yii provides is,
http://localhost/connect_donors/index.php?r=controllerId/functionName
We used the urlManager in /connect_donors/protected/config/main.php to configure the SEO friendly url's..
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>'
Now the URL that was working was
http://localhost/connect_donors/index.php/controllerId/functionName
Then I used the .htaccess file to remove index.php from the above URL.
Location of .htaccess is : /connect_donors/.htaccess
Following is .htaccess file,
RewriteEngine On
RewriteBase /connect_donors
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\?*$ index.php/$1 [L,QSA]
URL Chenged to :
http://localhost/connect_donors/controllerId/functionName
Everything working fine and awesome.
But yesterday I uploaded the application on production server.
On Production Server
Everything remained same only I had to change the .htaccess file.
The .htaccess file on server is,
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\?*$ index.php/$1 [L,QSA]
Now the following url:
http://donorsconnect.com/
loads the home page of server properly.
But,
http://donorsconnect.com/profile
redirects again to home page.
NOTE : There is no session set on the "profile" controller.
class ProfileController extends CController
{
public function actionIndex()
{
$this->render('index');
}
}
I tried lot of things, changing the .htaccess file to different codes. but none helped me.
Any help is appreciable.
Solution
I finally got the solution and the mistake I had done.
My Components had a request array containing baseUrl.
'components'=>array(
...
'request' => array(
'baseUrl' => 'http://donorsconnect.com',
...),
Due to this it was not loading. I did not find the real reason for that.
But after removing that 'request' array, its loading fine.
Check link,
http://donorsconnect.com/profile
You should not hide index.php like this, read carefully : http://www.yiiframework.com/doc/guide/1.1/en/topics.url#hiding-x-23x
You should set showScriptName to false in your main config :
'urlManager'=>array(
.....
'showScriptName'=>false,
.....
),
And your .htaccess should look like this :
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

what is wrong with this .htaccess and Yii urlManager settings?

I have a yii website under a sub directory e.g
http://localhost/~username/maindirectory/yiiapp/
My htaccess file:
DirectoryIndex index.php index.html index.htm
Options +FollowSymLinks
IndexIgnore */*
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /yiiapp
# 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
</IfModule>
protected/config/main.php
...
'urlManager'=>array(
'urlFormat'=>'path',
'showScriptName'=>false,
....
The issue:
let say I accesss :
step one: http://localhost/~username/maindirectory/yiiapp/
step two: click any link on home page (default yii app) let say About Us page
step three: http://localhost/~username/maindirectory/yiiapp/site/page?view=about
step four : click on any on the link (let say same page)
url will look like : http://localhost/yiiapp/site/page?view=about
so : all links are accessible as : http://localhost/yiiapp/.... , instead of removing index.php from link the while string b/w localhost and base directory is removed .
I tried this already and need the same sort of url on localhost , obviously without using sub domains
please help me fix this .
Partial answer:
The problem is that you are redirecting to an absolute URL on the same domain.
The URL probably looks like /yiiapp/site/page?view=about. However that leads to http://localhost/yiiapp/site/page?view=about. You must prepend the website directory relative to the domain (not sure if I expressed properly).
For example, the URL in the a href tag should look like /~username/maindirectory/yiiapp/site/page?view=about so you must prepend the /~username/maindirectory part from somewhere.
Have your tried:
'urlManager'=>array(
....
'baseUrl' => '/~username/maindirectory/yiiapp',
....
http://www.yiiframework.com/doc/api/1.1/CUrlManager#setBaseUrl-detail
so RewriteBase /~username/maindir/yiiapp , in .htaccess fixed the issue , not the
'baseUrl' => '/~username/maindirectory/yiiapp' thing in config/main.php
Here is the full .htaccess file:
DirectoryIndex index.php index.html index.htm
Options +FollowSymLinks
IndexIgnore */*
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /~username/maindir/yiiapp
# 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
</IfModule>

yii, mamp, htaccess rewrite urls

I am trying to rewrite yii urls but with no luck. I have spent hours going through sites and come up with the same answer each time which doesn't work for me:
http://www.yiiframework.com/doc/guide/1.1/en/topics.url#user-friendly-urls
I would like to resolve the urls to basic paths e.g
/index.php/site/index
to
/
/index.php/ads and /index.php/ads/
to
/ads
/ads/details?ad=9
to
/ads/9
The problem seems to be that the .htaccess has no effect.
I am using:
mamp pro
on a mac with lion
and the web directory is different to the webserver root.
I have set AllowOveride through the console.
The .htaccess is in the same folder as the main index.php but doesn't register even if I create an error.
I have had no problem with other non-yii web directories using an htaccess file
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ /index.php
//main.php
'urlManager'=>array(
'urlFormat'=>'path',
'showScriptName'=>false,
'rules'=>array(
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
),
),
I have a project where my Yii application is in a subfolder as well.
This is my .htaccess file:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\?*$ index.php/$1 [L,QSA]
Then in main.php, you'll need to add these lines to the 'rules' array:
'' => 'site/index', // This is for the home page
Is ads the name of your controller? In other words, do you have a file named AdsController.php in your controllers folder? If so, then it will work by default.
Just curious why you're hitting the index.php directly? You should be able to setup your localhost to point directly to your Yii app folder. For example, on my machine I created http:// mytestapp/ and it points to the app's folder in htdocs.
Since you're on a Mac, you need to edit your etc/hosts file as well...
I added a line like this:
127.0.0.1 mytestapp
Hope that helps...

How to remove index.php from URLs in Kohana 3.1

Currently, using Kohana 3.1, I can access my controllers using:
http://localhost/kohana/index.php/admin
However, I would like to access them without the "index.php" in the middle, as in:
http://localhost/kohana/admin
How can I do that? Do I need to change my .htaccess file or some config option?
I'm using the .htaccess provided with Kohana:
# Turn on URL rewriting
RewriteEngine On
# Installation directory
RewriteBase /
# Protect hidden files from being viewed
Order Deny,Allow
Deny From All
# Protect application and system files from being viewed
RewriteRule ^(?:application|modules|system)\b.* index.php/$0 [L]
# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite all other URLs to index.php/URL
RewriteRule .* index.php/$0 [PT]
However, if you use Rewritebase /kohana/, you will still get index.php in the url.
I'm using Kohana 3, so I go to bootstrap.php and change this:
Kohana::init(array(
'base_url' => '/',
'index_file' => '',
));
Change the RewriteBase directive to where the application is. Yours would be:
# Installation directory
RewriteBase /kohana/
Also make sure you have the correct base_url in your bootstrap.
On some website I saw .htaccess suggestion:
# Rewrite all other URLs to index.php/URL
RewriteRule .* index.php?kohana_uri=$0 [PT,L,QSA]
It seems that you miss kohaha folder.
It should be like this:
Kohana::init(array(
'base_url' => '/kohana/',
'index_file' => '',
));
Turn on URL rewriting by adding the following code:
In .htaccess:
RewriteEngine On
# Installation directory
RewriteBase /
In Bootstrap:
Kohana::init(array( 'base_url' => '', 'index_file' => '' ));
You need to use 'index_file' => FALSE in bootstrap.php
Source : http://kohanaframework.org/3.0/guide/kohana/tutorials/clean-urls

Remove "/index/" from URIs in Kohana 3

As of right now all of my controllers are mapped like this:
http://example.com/index/index
http://example.com/index/services
http://example.com/index/contact
What I want to do is change the configuration of the URI to look like this:
http://example.com/index
http://example.com/services
http://example.com/contact
Here is my .htaccess file
# Turn on URL rewriting
RewriteEngine On
# Installation directory
RewriteBase /
# Protect hidden files from being viewed
<Files .*>
Order Deny,Allow
Deny From All
</Files>
# Protect application and system files from being viewed
RewriteRule ^(?:application|modules|system)\b.* index.php/$0 [L]
# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite all other URLs to index.php/URL
RewriteRule .* index.php/$0 [PT,L]
My bootstrap.php has the following:
Kohana::init(array(
'base_url' => '/',
'index_file' => FALSE
));
How can this be achieved?
Your .htaccess code looks fine. Check that your Apache configuration is fine:
Kohana URL rewriting
Also, you need to edit your application/bootstrap.php and setup the initialization variables there.
Kohana::init( array(
'base_url' => '/',
'index_file' => FALSE,
) );
You may want to check the user guide, there is a page just for this setup.
http://kohanaframework.org/3.0/guide/kohana/tutorials/clean-urls
I assume that you use action_index, action_services and action_contact inside Controller_Index.
If so, all you need to do is remove the controller section from routes (in your bootstrap.php)
Route::set('posttype', '<param>', array('action' => 'index|services|contact'))
->defaults(array(
'controller'=> 'index',
'action' => 'index'
));

Resources