- Url hide index php
- Url hide index php
- LoadModule rewrite_module modules/mod_rewrite.so ¶
- if a directory or a file exists, use it directly ¶
- otherwise forward it to index.php ¶
- Revisions
- Related Articles
- Как убрать index.html из URL
- Как убрать index.html или index.php через .htaccess
- Url hide index php
- LoadModule rewrite_module modules/mod_rewrite.so ¶
- if a directory or a file exists, use it directly ¶
- otherwise forward it to index.php ¶
- Revisions
- Related Articles
Url hide index php
Here is my experiment inspired by my own question and others from the forum.
I am not talking about [[Url: hide index.php](http://www.yiiframework.com/wiki/214/url-hide-index-php «this»)] or [[How to hide index.php on nginx]](http://www.yiiframework.com/wiki/15/how-to-hide-index-php-on-nginx «this»), however, i am also talking about ‘hide’ something.
take yii default scaffolding web app as example: assuming that you already did the trick as above to hide index.php from your uri, you might notice that the following two URLs are still working.
/webroot/site/index /webroot/index.php/site/index
someone in the forum suggested add the following code in /protected/components/Controller.php
public function init()< if (strpos(Yii::app()->request->requestUri, '/index.php') !== false)< throw new CHttpException(404,'The requested page does not exist.'); > >
I am not sure if I like the idea of throwing an error, so I took another approach [redirect], so modified the code as:
public function init()< if (Yii::app()->urlManager->showScriptName == false)< if (strpos(Yii::app()->request->requestUri, '/index.php') !== false)< $_uri = str_replace("/index.php", "", Yii::app()->request->requestUri); $_uri = str_replace("//", "", $_uri); $this->redirect($_uri); > > >
then you will always see url like this which ‘truelly’ hide index.php
good enough? just wait a minute, someone mentioned in the forumn he/she does not really like to see ‘index’ in this url even! he/she expects this:
hmmmm, would redirect do the trick? let’s put some code in /protected/controller/SiteController.php:
protected function beforeAction($action) < $_uri = false; if (Yii::app()->urlManager->showScriptName == false)< if (strpos(Yii::app()->request->requestUri, '/index.php') !== false)< $_uri = str_replace("/index.php", "", Yii::app()->request->requestUri); > if (Yii::app()->controller->action->id == 'index')< if (!$_uri) < if (strpos(Yii::app()->request->requestUri, "/index") !== false)< $_uri = str_replace("/index", "", Yii::app()->request->requestUri); > > else < if (strpos($_uri, "/index") !== false)< $_uri = str_replace("/index", "", $_uri); > > > > if ($_uri !== false)< $this->redirect($_uri); > return parent::beforeAction($action); >
I’m not sure if this is ‘the’ solution, but sure it will work for those ‘picky’ developers or users for a clean url.
You may ask why don’t you have the above code in Contoller Class [/protected/components/Controller.php], so this trick works for all Controllers? I tried, did work. I guess the redirection lost its way to hit SiteController. Someone may help me on this.
and feel free to modify this wiki.
Url hide index php
Maybe it looks simple, but some time ago I need some time to find a solution of this case. I finally got it, and I want to share that I also experienced such cases. So that it can make reference.
Neither let us do the steps:
- Make sure that the hosting / your pc mod_rewrite module is active. if not actively try to activate in a way, open the httpd.conf file. You can check this in the phpinfo.php to find out.
LoadModule rewrite_module modules/mod_rewrite.so ¶
LoadModule rewrite_module modules/mod_rewrite.so ~~~
RewriteEngine On RewriteBase / RewriteCond % !-f RewriteCond % !-d RewriteRule ^(.*)\?*$ index.php/$1 [L,QSA]
if above does not work try with this: ~~~ RewriteEngine on
if a directory or a file exists, use it directly ¶
otherwise forward it to index.php ¶
* move .htaccess file to root directory, where is index.php thare.
- protected
- index.php
- .htaccess
* Set main.php in your yii configuration, at component :
'urlManager'=>array( 'urlFormat'=>'path', 'showScriptName'=>false, 'caseSensitive'=>false, ),
For reference material I managed to do in my personal website (check my profile).
NB: This for hide index.php
Revisions
Related Articles
more on mod_rewrite
From the site — «mod_rewrite is used for rewriting a URL at the server level, giving the user output for that final page. So, for example, a user may ask for http://www.somesite.com/widgets/blue/, but will really be given http://www.somesite.com/widgets.php?colour=blue by the server.»
Unclear purpose, example and .htaccess
Hi,
your article is a bit unclear about what is the goal you are trying to reach? Perhaps 1-2 examples would make things clearer:-) Also what is the final content of .htaccess file? (you may want to put it into solid code block pretty formatted).
Cheers
Lubos
hide index.php
showScriptName will not suffice?
Thanx for pretty formatting, now it’s clearer what you want to achieve.
Yet, the question is:
What is the benefit over setting the built-in paramater for urlManager in configuration:
thank you, you are right.
This can not be run if not in the main config settings.
~~~
Set main.php in your yii configuration, at component :
[php]
‘urlManager’=>array(
'urlFormat'=>'path', 'showScriptName'=>false,
Not only check mod_rewrite instalation
To get mod_rewrite working not only modrewrite must be installed, but also check in the Apache directory config (/etc/apache2/sites-enabled/000-default_ on Ubuntu installed via apt-get) if the rule «AllowOverride None» exists in your project’s directory config. If so, change it to «AllowOverride All«. I have spent hours because of this thing, so it’s my tip to get things working.
RewriteBase PROJECTNAME
I am using WAMP, it works after i include the line:
win32 apache config
As thiromi said, make sure that you change the «AllowOverride» flag to «All»
# # AllowOverride controls what directives may be placed in .htaccess files. # It can be "All", "None", or any combination of the keywords: # Options FileInfo AuthConfig Limit # **AllowOverride All**
Hello, May you take a look on my yii topic
Hello, May you take a look on my yii topic
yiiframework.com/forum/index.php/topic/33491-user-friendly-urls/pagep161195fromsearch1#entry161195
I have made what is required but it does not work.
I dont understand why.
That Sounds good
Thanks for this great article
I just tried it but I fail first because I forget and put this variables
'urlFormat'=>'path', 'showScriptName'=>false, 'caseSensitive'=>false,
inside the array of the rules
🙂
I just a newbie with YII 🙂 so I’m just confused
One more thing.
Also, if you’re on a virtual directory (e.g. using
on your apache configuration) you MUST use a
statement on your .htaccess file, other than the rewrite won’t work again.
by «moving .htaccess to webroot» you mean delete .htaccess within protected folder? if so it’s horrible for security cause the protected folder is accessible for anyone.
i think you mean copy that .htaccess and use it for this urlManager and keep the other one.
Nice Article
Very Nice article ..thank you so much this works for me..
how to change the URL path?
how to change Url path like —
17695 Got it
i found the solution, simply add below script into urlManager array in main config file.
Please only use comments to help explain the above article.
If you have any questions, please ask in the forum instead.
Как убрать index.html из URL
Предположим, вы заказали бесплатный хостинг для сайтов html в Рег.ру. По умолчанию когда вы открываете сайт в браузере, веб-сервер указывает в конце домена название индексного файла «index.html» или «index.php». Это негативно сказывается на поисковой позиции сайта. Для успешного продвижения в поисковых системах потребуется настройка переадресации с удалением «index.html» или «index.php» в конце адреса вашего сайта. Например, перенаправление с сайта «faq-reg.ru/index.html» на «faq-reg.ru»
Как убрать index.html или index.php через .htaccess
Откройте файл .htaccess в корневой директории сайта. Если у вас нет этого файла воспользуйтесь справкой: У меня нет файла .htaccess, что делать?
RewriteEngine On RewriteRule ^index\.html$ / [R=301,L]
RewriteEngine On RewriteRule ^index\.php$ / [R=301,L]
Если у вас несколько файлов index.html в разных папках, например, faq-reg.ru/support/index.html, вы можете убрать index.html из адресной строки, применив правило:
RewriteEngine On RewriteRule ^index\.html$ / [R=301,L] RewriteRule ^(.*)/index\.html$ /$1/ [R=301,L]
Если у вас несколько файлов index.php в разных папках, например, faq-reg.ru/support/index.php, вы можете применить правило:
RewriteEngine On RewriteRule ^index\.php$ / [R=301,L] RewriteRule ^(.*)/index\.php$ /$1/ [R=301,L]
Готово, после внесения правила в .htaccess ваш сайт будет открываться без index.html или index.php в конце URL.
Url hide index php
You are viewing revision #9 of this wiki article.
This version may not be up to date with the latest version.
You may want to view the differences to the latest version.next (#10) »
Maybe it looks simple, but some time ago I need some time to find a solution of this case. I finally got it, and I want to share that I also experienced such cases. So that it can make reference.
Neither let us do the steps:
- Make sure that the hosting / your pc mod_rewrite module is active. if not actively try to activate in a way, open the httpd.conf file. You can check this in the phpinfo.php to find out.
LoadModule rewrite_module modules/mod_rewrite.so ¶
LoadModule rewrite_module modules/mod_rewrite.so ~~~
RewriteEngine On RewriteBase / RewriteCond % !-f RewriteCond % !-d RewriteRule ^(.*)\?*$ index.php/$1 [L,QSA]
if above does not work try with this: ~~~ RewriteEngine on
if a directory or a file exists, use it directly ¶
otherwise forward it to index.php ¶
* move .htaccess file to root directory, where is index.php thare.
- protected
- index.php
- .htaccess
* Set main.php in your yii configuration, at component :
'urlManager'=>array( 'urlFormat'=>'path', 'showScriptName'=>false, 'caseSensitive'=>false, ),
For reference material I managed to do in my personal website (check my profile).
NB: This for hide index.php
Revisions
Related Articles
more on mod_rewrite
From the site — «mod_rewrite is used for rewriting a URL at the server level, giving the user output for that final page. So, for example, a user may ask for http://www.somesite.com/widgets/blue/, but will really be given http://www.somesite.com/widgets.php?colour=blue by the server.»
Unclear purpose, example and .htaccess
Hi,
your article is a bit unclear about what is the goal you are trying to reach? Perhaps 1-2 examples would make things clearer:-) Also what is the final content of .htaccess file? (you may want to put it into solid code block pretty formatted).
Cheers
Lubos
hide index.php
showScriptName will not suffice?
Thanx for pretty formatting, now it’s clearer what you want to achieve.
Yet, the question is:
What is the benefit over setting the built-in paramater for urlManager in configuration:
thank you, you are right.
This can not be run if not in the main config settings.
~~~
Set main.php in your yii configuration, at component :
[php]
‘urlManager’=>array(
'urlFormat'=>'path', 'showScriptName'=>false,
Not only check mod_rewrite instalation
To get mod_rewrite working not only modrewrite must be installed, but also check in the Apache directory config (/etc/apache2/sites-enabled/000-default_ on Ubuntu installed via apt-get) if the rule «AllowOverride None» exists in your project’s directory config. If so, change it to «AllowOverride All«. I have spent hours because of this thing, so it’s my tip to get things working.
RewriteBase PROJECTNAME
I am using WAMP, it works after i include the line:
win32 apache config
As thiromi said, make sure that you change the «AllowOverride» flag to «All»
# # AllowOverride controls what directives may be placed in .htaccess files. # It can be "All", "None", or any combination of the keywords: # Options FileInfo AuthConfig Limit # **AllowOverride All**
Hello, May you take a look on my yii topic
Hello, May you take a look on my yii topic
yiiframework.com/forum/index.php/topic/33491-user-friendly-urls/pagep161195fromsearch1#entry161195
I have made what is required but it does not work.
I dont understand why.
That Sounds good
Thanks for this great article
I just tried it but I fail first because I forget and put this variables
'urlFormat'=>'path', 'showScriptName'=>false, 'caseSensitive'=>false,
inside the array of the rules
🙂
I just a newbie with YII 🙂 so I’m just confused
One more thing.
Also, if you’re on a virtual directory (e.g. using
on your apache configuration) you MUST use a
statement on your .htaccess file, other than the rewrite won’t work again.
by «moving .htaccess to webroot» you mean delete .htaccess within protected folder? if so it’s horrible for security cause the protected folder is accessible for anyone.
i think you mean copy that .htaccess and use it for this urlManager and keep the other one.
Nice Article
Very Nice article ..thank you so much this works for me..
how to change the URL path?
how to change Url path like —
17695 Got it
i found the solution, simply add below script into urlManager array in main config file.
Please only use comments to help explain the above article.
If you have any questions, please ask in the forum instead.