Thinkphp5 部署Apache URL重写问题

  • A+
所属分类:ThinkPHP5

我们写好的代码往往部署于Linux上,为了安全和URL的美观,经常会利用 Apache或者 Nignx 进行URL 重写。

ThinkPHP5 官方URL重写说明: Thinkphp5 URL重写

下面介绍几种Apache URL 重写的配置:

官方配置:

<IfModule mod_rewrite.c>
Options +FollowSymlinks -Multiviews
RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?/$1 [QSA,PT,L]</IfModule>

以上配置可能在部分PHP 版本或 Apache 版本上不起作用,可使用如下配置:

<IfModule mod_rewrite.c>
Options +FollowSymlinks -Multiviews
RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]</IfModule>

基本可以解决配置问题,但是在此配置下,如果打印 Request::instance()->param() 就会发现, pathinfo会被当做参数:

array(1) { ["/login_xhtml"]=> string(0) "" }

在某些情况下,我们需要获取所有的参数而不是pathinfo,因此,我们可以采取第三种配置方式:

<IfModule mod_rewrite.c>
Options +FollowSymlinks -Multiviews
RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [L,E=PATH_INFO:$1]

此时,打印 Request::instance()->param(),就可以发现,没有了这个参数:

array(0) { }

avatar

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: