我有以下网址 http://localhost.com/projectname/index.php?r=site/cms&view=about_us 我想更改此URL如下 http://localhost.com/projectname/about_us, 使用htaccess和urlmanager然后需要做什么?
答案 0 :(得分:1)
由于 Priya jain 已经给出了关于从您的Url隐藏index.php的答案。要将您的网址更改为http://localhost.com/projectname/about_us,您可以在 config / main.php
中进行以下更改'urlManager'=>array(
'urlFormat'=>'path',
'showScriptName'=>false,
'rules'=>array(
// add this line
'<view:\w+>'=>'site/cms',
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
),
),
如果您仍然无法隐藏index.php ,那么您可以试试这个
<ifModule mod_rewrite.c>
# Turn on the engine:
RewriteEngine on
# Don't perform redirects for files and directories that exist:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# For everything else, redirect to index.php:
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
</ifModule>
注意: - 将其另存为您的项目/ .htaccess ,并记住 showScriptName 应为 false 你的urlManger array() in config/main.php
答案 1 :(得分:0)
尝试在url中删除此代码index.php部分并正常工作:
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]
答案 2 :(得分:0)
创建.htaccess文件
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\?*$ index.php/$1 [L,QSA]
如果不起作用,请尝试
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
不要忘记在protected / config / main.php文件中取消注释urlmanager
答案 3 :(得分:0)
首先你应该像这样编辑main / config.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>',
),
),
(此步骤可以帮助您隐藏index.php)
第二,在文档中创建一个.htaccess文件,打印出来:
选项+ FollowSymLinks IndexIgnore / RewriteEngine on RewriteBase /
# 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
PS:这是我的配置,它可以帮助您的网址清晰显示:
'rules' => array(
'/login' => 'user/account/login',
'/logout' => 'user/account/logout',
'/registration' => 'user/account/registration',
),