YII URL how to generate new URL - php

I have one site in which I want URL like SiteURL/about.html.
I have changed code in config/main.php but when I login to site then gets session expired when I navigate to any other page.
If comment this code 'showScriptName'=>false in config/main.php file, then its work fine and now my login session is saved and us not expired however now my URL is becoming like this SiteURL/index.php/about.html
But I want URL like about.html.
I have also one another controller for game option which URL is like: http://www.demo.com/index.php/Games/info/9.html but I want URL like SiteURL/black-hook.html in this "black-hook" is the name of the game.
'urlManager'=>array(
'urlFormat'=>'path',
'showScriptName'=>FALSE,
'rules'=>array(
'<action>'=>'site/<action>',
'<view>'=>'array('site/page')',
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>'
),
),
*SiteURL = http://www.demo.com

First to get .html appended to your pages you need to use fake url suffix option in urlManager
This can be done by adding 'urlSuffix' =>'.html' to your urlManager config like this
'urlManager'=>array(
'urlFormat'=>'path',
'showScriptName'=>false,
'urlSuffix'=>'.html',
'rules'=>array(
'<action>'=>'site/<action>',
'<view>'=>'site/page',
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>'
),
),
Secondly for hiding index.php (called the entry script) in your URL in addition to changing configuration here you need to change configuration in your apache/nginx server.
Create the file /.htaccess with the following content and save it
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
Next verify if the AllowOveride All is there for your app directory in your apache configuration file You can also add the above configuration directly to your apache configuration file using Directory element without using .htaccess .
Note: For this to work you need to have rewrite engine enabled in apache config, You can do so with the following set of commands
a2enmod rewrite
Finally Restart apache2 after
/etc/init.d/apache2 restart
or
service apache2 restart

Related

How can I hide the index.php file for yii in apache/.htaccess/mod_rewrite?

I have this htaccess file:
RewriteEngine on
#if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
And I'm using Yii 1.1.15 (this site was taken from 1.1.13 version but such version is not available anymore for download, so I'm using it in a 1.1.15 framework install - althought it has no problem as far as I can see), having a conf/main.php file like:
<?php
return array(
'basePath'=>dirname(dirname(__FILE__)),
'name'=>'Grand Duval - Códigos premiados',
'defaultController'=>'participantes/create',
'language'=>'es',
// preloading 'log' component
'preload'=>array('log'),
// autoloading model and component classes
'import'=>array(
'application.models.*',
'application.components.*',
),
'modules'=>array(
'gii'=>array(
'class'=>'system.gii.GiiModule',
'password'=>'mypassword',
'ipFilters'=>array('127.0.0.1', '::1'),
),
),
// application components
'components'=>array(
'user'=>array(
// enable cookie-based authentication
'allowAutoLogin'=>true,
),
'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>',
),
),
'db'=>array(
'connectionString' => 'mysql:host=localhost;dbname=mydatabase',
'emulatePrepare' => true,
'username' => 'sbuser',
'password' => 'dbpassword',
'charset' => 'utf8',
),
'errorHandler'=>array(
'errorAction'=>'site/error',
),
'log'=>array(
'class'=>'CLogRouter',
'routes'=>array(
array(
'class'=>'CFileLogRoute',
'levels'=>'error, warning',
),
),
),
),
// application-level parameters that can be accessed
// using Yii::app()->params['paramName']
'params'=>array(
// this is used in contact page
'adminEmail'=>'webmaster#example.com',
),
);
The important part here is:
'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>',
),
),
Such config is intended - and does work like that - to generate absolute urls like http://www.mydomain.dev/controller/action for controller controller and action action.
I have the problem with the .htaccess file: If I try to hit http://www.mydomain.dev/controller/action, the url is not found. However if I hit http://www.mydomain.dev/index.php/controller/action it is found. It looks that .htacccess has no effect at all (It is the exact same if I delete the .htaccess file).
Q: what am I doing wrong?
I think you need to enable mod_rewrite try:
a2enmod rewrite
Then just restart your server.
I found the answer. After ensuring that mod_rewrite was active for my other sites I asked myself: Is my .htaccess being recognized?
So i wanted to force a 500 error by corrupting my .htaccess file:
RewriteEngine on
# if a directory or a file exists, use it directly
TryToForceA500
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
And no error was triggered! So I asked myself -again-: Why isn't it being recognized? and checked my site .conf file and found:
AllowOverride none
And changed to:
AllowOverride all
(this is just a dev server, so that setup is fine for me)
And then the file was recognized and worked!

Kohana 404 error: Requested URL not found

.
Hi everyone,
I'm working on a website for a school project.
Recently I've purchased me own webspace at Biberbit.com and wanted to upload my website.
When I upload it I see the following error on screen:
Kohana_HTTP_Exception [ 404 ]: The requested URL / was not found on this server.
My project uses the Kohana framework version 3.3.1
I already have the .htaccess file and the filenames/directory names are all lower case.
I am using the default route:
Route::set('default', '(<controller>(/<action>(/<id>)))')
->defaults(array(
'controller' => 'welcome',
'action' => 'index',
));
Originally I thought it might have been the default_url in the bootstrap.php file so I've changed those lines to this:
Kohana::init(array(
'base_url' => '/',
'index_file' => FALSE,
));
I've looked for the solution on multiple websites and couldn't find one that works for me.
Can anyone think of something that I might be missing?
I'm also using URL::site() for my links so I don't think that's causing it.
You can check the error and/or website at http://www.biberbit.com
EDIT:
.htaccess as requested
# 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]
The base_url is set wrong, it should be empty (as you are on the web root). Also depending on whether or not you have Linux, filenames must not be all lowercase but however you use them in the code. So Welcome.php and not welcome.php for the default controller.
Kohana_HTTP_Exception [ 404 ]: The requested URL / was not found on this server. exception is usually thrown when Kohana couldn't find corresponding action method to process a request.
Make sure you have Controller_Welcome class and action_index method in it.
Also you can inspect your Exception traceback and find what exactly caused an Exception. Most likely you'll see Exeption in SYSPATH/classes/Kohana/Controller.php file in if(!method_exists(...)) block.

Kohana URL not found

Hi i have a problem with my kohana code it's getting me the same error and i think there's a problem with the htaccess or bootstrap
It's installed on my root directory not as a subfolder.
Kohana_HTTP_Exception [ 404 ]: The requested URL / was not found on this server.
SYSPATH/classes/Kohana/Request/Client/Internal.php [ 79 ]
74 if ( ! class_exists($prefix.$controller))
75 {
76 throw HTTP_Exception::factory(404,
77 'The requested URL :uri was not found on this server.',
78 array(':uri' => $request->uri())
79 )->request($request);
80 }
81
82 // Load the controller using reflection
83 $class = new ReflectionClass($prefix.$controller);
84
SYSPATH/classes/Kohana/Request/Client.php [ 114 ] » Kohana_Request_Client_Internal->execute_request(arguments)
SYSPATH/classes/Kohana/Request.php [ 990 ] » Kohana_Request_Client->execute(arguments)
DOCROOT/index.php [ 109 ] » Kohana_Request->execute()
Here's my htaccess code
# 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]
#-SetEnv KOHANA_ENV "production"
and in the bootstrap i have set the site_url to '/'
I had same problem when I moved kohana from localhost to linux hosting.
Try to rename your controller files in capital letters. In bootstrap routes keep small letters.
It helped me.
Please try and post much of the bootstrap.php file as possible. Chances are that you do not have a default root set.
Search within your file for a code block that looks like
Route::set('default', '(<controller>(/<action>(/<id>)))')
->defaults(array(
'controller' => 'default',
'action' => 'index',
));
The keyword being Route::set('default', '(<controller>(/<action>(/<id>)))')
Where is your application hosted? Is it on www or within a subfolder in www?
What you're seeing is the error that is thrown when you haven't set up your environment to run for the first time to use clean urls. Do this:
go into your .htaccess file and set the correct path for RewriteBase
if your localhost path where localhost/my/site, your htaccess should be:
RewriteBase /localhost/my/site/
(make sure you rename the file so it is ONLY called .htaccess )
Go into your bootstrap.php file and make sure Kohana::init has:
'base_url' => '/localhost/my/site/' in its array.
Inside the /application/classes/Controller/ directory, add your controller file name the file so that the first letter is uppercase (i.e. Welcome.php) and inside the controller, make sure your controller contents are written correctly.
/application/classes/Controller/Welcome.php
That's a good start towards solving the problem.
This exception was throwed because controller not found. By default, '/' URI means calling method action_index() of Controller_Welcome. See bootstrap.php, Route::set('default', ...).
I think, there can be one of these reasons:
You have default route, and application/classes/Controller/Welcome.php was removed.
While upgrading Kohana version to v3.3, you havent replace application folder with new one. So, you have application/classes/controller/welcome.php instead of application/classes/Controller/Welcome.php.
You have changed default route with non-existing controller. Note that Kohana v3.3 has new file/class naming conventions.

Yii can't start gii

I am developing website with PHP Yii Framework and I am now stack, I need to start gii, but I can't do this. when i type www.example.com/index.php/gii or www.example.com/gii it gives me this error :
/gii/default/login // <- website redirects to here
This webpage has a redirect loop
The webpage at http://www.example.com/gii/default/login has resulted in too many redirects.
Clearing your cookies for this site or allowing third-party cookies may fix the problem.
If not, it is possibly a server configuration issue and not a problem with your computer.
I don't think that the error is because of modified htaccess and main configuration, but anyway here is main.php configuration file:
'urlManager'=>array(
'urlFormat'=>'path',
'showScriptName'=>false,
'rules'=>array(
'site/page/<view:\w+>'=>'site/page',
'<controller:\w+>/<cact:\w+>/<action:\w+>'=>'<controller>/<cact>',
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
),
),
and .htaccess :
Options +FollowSymLinks
IndexIgnore */*
RewriteEngine on
#non-www to www
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
# 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 [L]
So can you help me, please?
To use this path: index.php?r=gii/default/login , you must turn OFF the url manager in /protected/config/main.php
Also check urlManager's rules. That was the issue for me:
'components' => array(
'urlManager' => array(
'urlFormat' => 'path',
'showScriptName' => false,
'rules' => array(
// ...
// will handle `gii/default/login` uri and makes infinite redirection loop circle
'<controller:\w+>/<action:\w+>/<sub_action:\w+>'=>'<controller>/<action>',
// ...
),
),
),
Check if the gii module in your configuration file is there and it is uncommented.
If gii is not in there you should add it within the module array.
'modules'=>array(
'gii'=>array(
'class'=>'system.gii.GiiModule',
'password'=>***choose a password***
),
),
More info for gii here
Like FelikZ mentioned, this might be because you have created a third param in a rule that uses \w+ instead of the default \d+ and hence will match "gii" as the controller, "default" as the action and "login" as the ID (or sub action, or whatever is mentioned).
My rules looked like the following:
'<controller:\w+>/<action:\w+>/<id:\w+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
The fix is to add the following as the very first rule in order to make gii hit the right place:
'gii/<controller:\w+>/<action:[\w-]+>' => 'gii/<controller>/<action>',
Which should make your entire urlManager config look something like the following:
'urlManager'=>array(
'urlFormat'=>'path',
'showScriptName'=>false,
'rules'=>array(
'gii/<controller:\w+>/<action:[\w-]+>' => 'gii/<controller>/<action>',
'<controller:\w+>/<action:\w+>/<id:\w+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
),
),
This problem occurs because of the two sessions of the OP, for example cookie PHPSESSID there two domains domain .site.ru and admin.site.ru two different sessions. Delete PHPSESSID cookies and login to gii

Yii 1.1.7 - cannot find gii page

I want to use Gii in Yii. My protected/config/main.php for my first webapp has this part uncommented, as instructed in the Yii documentation to enable Gii (123.45.67.123 is my public IP address from the computer I am trying to access):
'modules'=>array(
// uncomment the following to enable the Gii tool
'gii'=>array(
'class'=>'system.gii.GiiModule',
'password'=>'123456',
// If removed, Gii defaults to localhost only. Edit carefully to taste.
'ipFilters'=>array('123.45.67.123','127.0.0.1','::1'),
),
),
I also have the urlManager enabled in my protected/config/main.php by uncommenting the below:
// uncomment the following to enable URLs in path-format
'urlManager'=>array(
'urlFormat'=>'path',
'rules'=>array(
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
),
),
When I go to my Yii site, for example, www.example.org, the basic Yii page is loaded fine. When I do www.example.org/gii, I get a 404. When I go to www.example.org/index.php?r=gii, I get a 404. Why is my Gii page not found? I am using CentOS 5.6.
Try:
http://www.example.org/index.php/gii
It seems you have the same rules as I do for url. If http://www.example.org brings you to your main yii webapp page then the above link should work.
You were going to http://www.example.org/gii which is incorrect.
Im using urlManager like this for gii
'urlManager'=>array(
'urlFormat'=>'path',
'cacheID' => false,
//'caseSensitive' => true,
'showScriptName' => false,
'urlSuffix' => '/',
'rules'=>array(
'gii'=>'gii',
'gii/<controller:\w+>'=>'gii/<controller>',
'gii/<controller:\w+>/<action:\w+>'=>'gii/<controller>/<action>',
...
and it doesn't conflict with routes for other site pages
Can you post your entire urlManager block with #briiC.lv's code incorporated? It should work, it's pretty much the same as mine:
'urlManager'=>array(
'urlFormat'=>'path',
'rules'=>array( // remove gii for production:
'gii'=>'gii',
'gii/<controller:\w+>'=>'gii/<controller>',
'gii/<controller:\w+>/<action:\w+>'=>'gii/<controller>/<action>',
'site/index'=>'/',
'<controller:\w+>'=>'site/<controller>',
'<filter:(proj|dept)>/<id:\d+>'=>'site/',
),
'showScriptName'=>false,
),
If it still doesn't work, you might also want to post/link to your complete main.config file.
Have you tried with the urlManager desactivated? Just to see if gii itself it's working
If you have mod rewrite enabled and want to place the following in your htaccess file
php_value upload_max_filesize 1M
DirectoryIndex index.php
Options +FollowSymlinks
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
and add the following to the 'urlManager' array in your config/main.php file:
'showScriptName'=>false,
Will remove the index.php from your url and make sure to only display urls in the form of domain.com/controller/action
I wasn't able to access www.example.org/gii until I added the following in my vhost config
<Directory /path/to/web/root>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
Allow from all
</Directory>
i have just had the same problem with the URL and Gii and i found out that i had written not the right IP Address. When i changed to the right one from the Network Statistics and then added the IP to the ipFilters of GII.
Hope that helps!

Categories