How to remove index.php from URL - yii

My yii application was installed in virtual host(http://yii.loc) (WAMP).
I need to remove index.php from url. But only http://yii.loc url works, the other pages says "Not Found".
'urlManager'=>array(
'urlFormat' => 'path',
'showScriptName' => false,
'rules' => array(
'<controller:\w+>/<id:\d+>' => '/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '/',
'<controller:\w+>/ <action:\w+>' => '/',
),
.htaccess
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

Check out the settings of Apache. I had same problems when the rewrite rule was disabled.
Go to WAMP icon->Apache modules->rewrite_module and turn it on
Than try to restart your WAMP & aplication in browser)

Got a solution. By mistake I have changed the .htaccess which are located in protected folder. So I restored the file
deny from all
The I created a new .htaccess file in the root directroy and add
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
And now it works.

Related

Cakephp routes.php stops working after I changed the content in the default .htaccess file. How to update .htaccess to resume basic url routing

e.g. my-domian/yyy yyy can't be recognized by my routes.php
I accidentally delete the content in my .htaccess file.
The routes.php file (router file) no longer route my site properly.
routes.php:
Router::scope('/', function ($routes) {
//this one works
$routes->connect('/', ['controller' => 'Pages', 'action' => 'display', 'home']);
//these two routes no longer work
$routes->connect('/survey', ['controller' => 'Surveys', 'action' => 'get']);
$routes->connect('/scenario', ['controller' => 'Scenarios', 'action' => 'get']); });
my-domain/ works.
my-domain/survey?xx=3 no longer works.
404 error I got for my-domain/survey?xx=3 :
File does not exist: /home/nexusuto/public_html/survey
my .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*/)?\.svn/ - [F,L]
ErrorDocument 403 "Access Forbidden"
RewriteCond %{HTTP_USER_AGENT} libwww-perl.*
RewriteRule .* – [F,L]
</IfModule>
Please help me... I have been stuck for a long time :(((( I just want to resume the basic use of .htaccess file i.e. I can route my-domain/survey to my survey controller..
If you do not know how to write a .htaccess rules, then simply downloading the same version of CakePHP and replace .htaccess in your web application.
If you have not modified the file routes.php just create .htaccess again
Try with this .htaccess structures:
ROOT .HTACCESS
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
app/ .HTACCESS
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /app/
RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/$1 [L]
</IfModule>
app/webroot/ .HTACCESS
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /app/webroot
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>
But if you modify routes file maybe you should try this:
$routes->connect('/survey/*', ['controller' => 'Surveys', 'action' => 'get']);
The URL is a normal slash delimited string, but can also contain a
wildcard (*) or Route Elements. Using a wildcard tells the router that
you are willing to accept any additional arguments supplied. Routes
without a * only match the exact template pattern supplied.
http://book.cakephp.org/2.0/en/development/routing.html#connecting-routes

Yii2 hide a web part in URL using a .htaccess

I have created a .htaccess file in my root folder that contains the following code:
-RewriteEngine On
Add a comment to this line
-RewriteCond %{REQUEST_URI} !^/web
-RewriteRule ^/?([^/]+)$ /web/$1 [L]
but it doesn't work as expected. I also have a .htaccess file in web folder that contains the following code:
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 am I doing wrong, here?
Put in .htaccess:
RewriteRule . /web/index.php
Or
RewriteRule . /basic/web/index.php
EDIT.
I find better solution. Put in .htaccess:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.domain.com$
RewriteCond %{REQUEST_URI} !web/
RewriteRule (.*) /web/$1 [L]
Change domain.com to your domain.
should change the AppAsset public $css path
Add .htaccess outside of the web folder
AddDefaultCharset UTF-8
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
Add following in config/web.php
'urlManager' => array(
'class' => 'yii\web\UrlManager',
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => array(
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
),
),
move index.php from inside of the web folder to outside and change these 3 lines
require(__DIR__ . '/vendor/autoload.php');
require(__DIR__ . '/vendor/yiisoft/yii2/Yii.php');
$config = require(__DIR__ . '/config/web.php');
This is working for me in both development and production.

How to remove index.php and ?r= in Yii Framework

How to remove "index.php" and also wants to remove "?r=". I want to make URLs user friendly.
Do these 3 steps:
Enable Url re-writing on Apache.
Place .htaccess on root of your project
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
In your configuration protected/config/main.php set showScriptName to false like this to your url manager components >> urlManager
'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>',
),
'showScriptName'=>false,
)
1) enable your urlManager in protected>>config>>main.php by adding these line to the array:
'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>',
),
'showScriptName'=>false,
),
2) The .htaccess file should be in the site root in the same of index.php level and should be like that:
RewriteEngine on
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php)
# otherwise forward it to index.php
RewriteRule ^(.+)$ index.php?$1 [PT,L,QSA]
3) make sure that your Apache configuration AllowOverride All not None

Yii - remove index.php from url - always redirected to 'index.php'

In my test application, I'm trying to remove index.php from the url. I can access the root page like so http://localhost/trackstar/ instead of http://localhost/trackstar/index.php, but as soon as I'm trying to access another page like http://localhost/trackstar/project or any other page I'm always ending up at the index.php page. Opening http://localhost/trackstar/index.php/project works however.
I'm using WAMP server. Configuration as below.
Rewrite module in apache is enabled.
Config main:
'urlManager' => array(
'urlFormat' => 'path',
'rules' => array(
'commentfeed' => array('comment/feed', 'urlSuffix' => '.xml', 'caseSensitive' => false),
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
),
'showScriptName' => false,
),
.htaccess in root folder of project:
Options +FollowSymlinks
IndexIgnore */*
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
I've also added in httpd.conf, although not really necessary I think:
<Directory "c:/wamp/www/TrackStar/">
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
allow from all
</Directory>
I've tried several combinations of extra parameters in .htaccess, but I keep getting redirected to my index.php page.
Any thoughts as to why this doesn't work?
Edit:
Since I'm being redirected, the checks in RewriteCond don't succeed, in other words the REQUEST_FILENAME isn't found as a directory or file. Maybe there's something off in my directory structure?
The TrackStar\protected\views\project directory, for example is the page I'm trying to access via http://localhost/trackstar/project. My Yii framework files are in a directory above the www folder.
Try to add into .htaccess following after RewriteEngine on:
RewriteBase /trackstar/
The problem in: when not found file it redirects to http://localhost/index.php, in request http://localhost/trackstar/index.php/project file index.php is found and it works correctly
UPDATED:
Try modify your urlManager settings (add /trackstar/):
'urlManager' => array(
'urlFormat' => 'path',
'rules' => array(
'commentfeed' => array('comment/feed', 'urlSuffix' => '.xml', 'caseSensitive' => false),
'/trackstar/<controller:\w+>/<id:\d+>' => '<controller>/view',
'/trackstar/<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'/trackstar/<controller:\w+>/<action:\w+>' => '<controller>/<action>',
),
'showScriptName' => false,
),
You can do that with 2 simple steps.
Configure the 'urlManager' at config/main.php to look like this
'urlManager'=>array(
'urlFormat'=>'path',
'showScriptName'=>false,
),
Add an .htaccess file at the root of the application with the following code
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
Thats it!
Can you try:
.htaccess in root folder of project:
Options +FollowSymlinks -MultiViews
IndexIgnore */*
DirectoryIndex index.php
RewriteEngine On
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.*?)index\.php$ /$1 [L,NC,R=302]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
change your config/main.php like below:
'urlManager'=>array(
'urlFormat'=>'path',
'showScriptName'=>false,
'caseSensitive'=>false,
'rules'=>array(
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
),
then place your htaccess file on base directory with protected folder (it may be inside protected folder) place below code to your htaccess file
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
or try with this---
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\?*$ index.php/$1 [L,QSA]
done...hope it will work

yii : how to config SEO friendly url at Yii?

I create a app by yiic. and then I tried to make a SEO url. I uncomment urlManager at [app root]/protected/config/main.php . And I add following into [app root]/.htaccess.
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
When I browse [app]/site/index, I got the error 404 - object not found. But if I browse by [app]/index.php?r=site/index, it is show me my app page. I followed many web sites by googling. but I got same error.
please help me to solve this one.
Try to use this :
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\?*$ index.php/$1 [L,QSA]
Instead of using .htaccess you can use urlManager in Yii to have custom url rules. Take a read at urlManager for details. I hope this post might also help to give you the idea. Controlling url in Yii: controller/action/id to /id
I found the solution by myself.
1. uncomment urlManager at app/protected/config/main.php
2. create .htaccess file under app/ folder. (Important : not under app/protected/ folder)
3. copy and past the following code into new .htaccess file
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
That all. Thank you for your helping me, everybody.
Check out this article on URL rewriting in Yii
Also, in your config, set showScriptName to false
'urlManager'=>array(
'urlFormat' => 'path',
'showScriptName' => false,
...