Apache开启 .htaccess , url_rewrite ,vhost

1. 开启.htaccess支持

将 httpd.conf 中
AllowOverride None
改为
AllowOverride All

2. 开启url_rewrite模块

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

3. 开启vhost功能

(1) 去掉 httpd.conf#Include conf/extra/httpd-vhosts.conf 前面的#
(2) 去掉 httpd.conf#LoadModule vhost_alias_module modules/mod_vhost_alias.so 前面是#
(3) 然后去配置 conf/extra/httpd-vhosts.conf 文件中, 根据示例配置虚拟主机

4. 常用的.htaccess配置

  1. <IfModule mod_rewrite.c>
  2. RewriteEngine on
  3. RewriteCond %{REQUEST_FILENAME} !-d
  4. RewriteCond %{REQUEST_FILENAME} !-f
  5. RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]
  6. </IfModule>

5. 常用站点配置

  1. # conf/extra/httpd-vhosts.conf
  2. <VirtualHost *:80>
  3. DocumentRoot "D:\Project\Coding\phpIDE"
  4. ServerName ide
  5. ServerAlias ide
  6. <Directory "D:\Project\Coding\phpIDE">
  7. Options FollowSymLinks ExecCGI
  8. AllowOverride All
  9. Order allow,deny
  10. Allow from all
  11. Require all granted
  12. </Directory>
  13. </VirtualHost>

别忘了在hosts文件中添加 127.0.0.1 ide

Nginx配置ThinkPHP的UrlRewrite和PathInfo模式

打开对应的站点配置文件, 找到下面的代码段:

  1. location ~ .*\.(php|php5)?$
  2. {
  3. fastcgi_pass 127.0.0.1:9000;
  4. fastcgi_index index.php;
  5. include fastcgi.conf;
  6. }

修改为

  1. location ~ .php
  2. {
  3. fastcgi_pass 127.0.0.1:9000;
  4. fastcgi_index index.php;
  5. include fastcgi.conf;
  6. #定义变量 $path_info ,用于存放pathinfo信息
  7. set $path_info "";
  8. #定义变量 $real_script_name,用于存放真实地址
  9. set $real_script_name $fastcgi_script_name;
  10. #如果地址与引号内的正则表达式匹配
  11. if ($fastcgi_script_name ~ "^(.+?\.php)(/.+)$") {
  12. #将文件地址赋值给变量 $real_script_name
  13. set $real_script_name $1;
  14. #将文件地址后的参数赋值给变量 $path_info
  15. set $path_info $2;
  16. }
  17. #配置fastcgi的一些参数
  18. fastcgi_param SCRIPT_FILENAME $document_root$real_script_name;
  19. fastcgi_param SCRIPT_NAME $real_script_name;
  20. fastcgi_param PATH_INFO $path_info;
  21. }
  22. location / {
  23. if (!-e $request_filename) {
  24. rewrite ^(.*)$ /index.php?s=$1 last;
  25. break;
  26. }
  27. }