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

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

Related

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.

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

How to remove index.php from URL

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.

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,
...

.htaccess rewrite advanced rule

I would like a .htaccess rewrite rule, but I can't figure out how to make it.
Examples
www.example.com/index.php => www.example.com/index.php
www.example.com/folder/profile.php => www.example.com/profile.php
www.example.com/folder => www.example.com
A rewrite rule that can serve files from a specific folder at top level, while still serving the top-level files.
here u go! try this
EDIT: made changes to exclude index.php!
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/folder/
RewriteRule ^(.*)$ folder/$1 [L]
i do the same for my Zend Framework projects , where i route all the requests from my http://domain.com/public/ to http://domain.com/
To redirect if a file does't exist, do this
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /folder/$1 [R]