您的位置:首页 > 编程语言 > PHP开发

php 去掉Url里的 index.php

2016-02-25 17:24 706 查看
php项目中,为了访问链接的友好性及SEO优化,我们经常需要为访问页面配置一个短链接,并把index.php去掉。

php所用的web服务器通常为:nginx或者apache。下面分别说一下两种服务器的不同配置方法

NGINX 配置:

1、打开nginx配置文件:/usr/local/nginx/conf/nginx.conf

2、修改文件,有两种语法

A:

location / {
try_files $uri $uri/ /index.php?q=$uri&$args;
}


B:

location / {
if (!-e $request_filename) {
rewrite ^/(.*)$  /index.php?q=$uri&$args;
}
}


如果根目录为二级目录,则需要做对应修改:

A:

location /cn/ {
try_files $uri $uri/ /cn/index.php?q=$uri&$args;
}


B:

location /cn/ {
if (!-e $request_filename) {
rewrite ^/cn/(.*)$  /cn/index.php?q=$uri&$args;
}
}


3、检查Nginx是否有语法错误,无错误则重启

#检查是否有语法错误
/usr/local/nginx/sbin/nginx -t
#重启nginx
/usr/local/nginx/sbin/nginx -s reload


Apache 配置:

1、httpd.conf配置文件中加载了mod_rewrite.so模块

#LoadModule rewrite_module modules/mod_rewrite.so   把前面的 # 去掉


2、修改httpd.conf配置文件:

AllowOverride None 中将None改为 All

(注意其他地方的AllowOverride也统统设置为ALL)

AllowOverride none  改   AllowOverride ALL
Options None
Order allow,deny
Allow from all


3、.htaccess文件必须放到跟目录下

.htaccess 文件内容

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: