正文 5114字数 387,599阅读

thinkphp url 去掉/index.php/ 404
为了更好的实现SEO优化,我们需要隐藏URL地址中的index.php,由于不同的服务器环境配置方法区别较大,apache环境下面的配置我们可以参考5.9 URL重写来实现,就不再多说了,这里大概说明下IIS和Nginx下面的基本配置方法和思路。
IIS环境
如果你的服务器环境支持ISAPI_Rewrite的话,可以配置httpd.ini文件,添加下面的内容:
RewriteRule (.*)$ /index\.php\?s=$1 [I]
Run code
Cut to clipboard

    在IIS的高版本下面可以配置web.Config,在中间添加rewrite节点:
    <rewrite> <rules> <rule name="OrgPage" stopProcessing="true"> <match url="^(.*)$" /> <conditions logicalGrouping="MatchAll"> <add input="{HTTP_HOST}" pattern="^(.*)$" /> <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" /> <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" /> </conditions> <action type="Rewrite" url="index.php/{R:1}" /> </rule> </rules> </rewrite>
    Run code
    Cut to clipboard

      Nginx环境
      在Nginx低版本中,是不支持PATHINFO的,但是可以通过在Nginx.conf中配置转发规则实现:
      location / { // …..省略部分代码 if (!-e $request_filename) { rewrite ^(.*)$ /index.php?s=$1 last; break; } }
      Run code
      Cut to clipboard

        其实内部是转发到了ThinkPHP提供的兼容模式的URL,利用这种方式,可以解决其他不支持PATHINFO的WEB服务器环境。

        如果你的ThinkPHP安装在二级目录,Nginx的伪静态方法设置如下,其中youdomain是所在的目录名称。
        location /youdomain/ { if (!-e $request_filename){ rewrite ^/youdomain/(.*)$ /youdomain/index.php?s=$1 last; } }
        Run code
        Cut to clipboard


          yourphp
          如果没有此文件请复制到网站根目录即可,否则去掉URL中/index.php会出现404错误,西数网站助手无需配置伪静态,不用选择
          web.config
          <?xml version="1.0" encoding="UTF-8"?> <configuration> <system.webServer> <rewrite> <rules> <rule name="已导入的规则 1" stopProcessing="true"> <match url="^(.*)$" ignoreCase="false" /> <conditions logicalGrouping="MatchAll"> <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" /> <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" /> </conditions> <action type="Rewrite" url="/index.php?s={R:1}" appendQueryString="true" /> </rule> <rule name="已导入的规则 2" stopProcessing="true"> <match url="^(.*)GZphp/Tpl/(.*).html$" ignoreCase="false" /> <action type="Rewrite" url="/403.html" appendQueryString="true" /> </rule> <rule name="已导入的规则 3" stopProcessing="true"> <match url="^(.*)/(.*).php$" ignoreCase="false" /> <action type="Rewrite" url="/403.html" appendQueryString="true" /> </rule> </rules> </rewrite> </system.webServer> </configuration>
          Run code
          Cut to clipboard

            其中.htaccess
            php_value memory_limit 200M php_value upload_max_filesize 200M php_value post_max_size 200M php_value max_execution_time 3600000 php_value max_input_time 3600000 <IfModule mod_security.c> SecFilterEngine Off SecFilterScanPOST Off </IfModule> Options +FollowSymLinks RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ /index.php?s=$1 [QSA,PT,L] RewriteRule ^(.*)GZphp/Tpl/(.*).html$ /403.html [QSA,PT,L]
            Run code
            Cut to clipboard

              RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ /index.php?s=$1 [QSA,PT,L] RewriteRule ^(.*)GZphp/Tpl/(.*).html$ /403.html [QSA,PT,L] RewriteRule ^(.*)/(.*).php$ /403.html [QSA,PT,L]
              Run code
              Cut to clipboard

                robots.txt
                User-agent: * Disallow: /Public/ Disallow: /GZphp/ Disallow: /Install/ Disallow: /Core/
                Run code
                Cut to clipboard

                  当有子目录网站是用,如 广西晟鼎投资有限公司 gxsdgroup.com
                  <?xml version="1.0" encoding="UTF-8"?> <configuration> <system.webServer> <rewrite> <rules> <rule name="OrgPage" stopProcessing="true"> <match url="^(.*)$" /> <conditions logicalGrouping="MatchAll"> <add input="{HTTP_HOST}" pattern="^(.*)$" /> <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" /> <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" /> </conditions> <action type="Rewrite" url="index.php/{R:1}" /> </rule> </rules> </rewrite> </system.webServer> </configuration>
                  Run code
                  Cut to clipboard