针对不同服务器, 虚拟空间, 运行 PHP 的环境也有所不同, 目前主要分为: Nginx,apache,IIS 以及其他服务器. 下面分享如何去掉 URL 上的 index.php 字符, 记得最后要重启服务器, 在管理后台清除缓存哦!
[IIS 服务器]
在网站根目录下有个 web.config 文件, 这个文件的作用是重写 URL, 让 URL 变得简短, 易于 SEO 优化, 以及用户的记忆. 打开 web.config 文件, 在原有的基础上加以下代码片段即可.
- <rewrite>
- <rules>
- <rule name="Imported Rule 1" enabled="true" 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>
以下是某个香港虚拟空间的效果:
- <?xml version="1.0" encoding="UTF-8"?>
- <configuration>
- <system.webServer>
- <handlers>
- <remove name="PHP-7.0-7i24.com" />
- <remove name="PHP-5.6-7i24.com" />
- <remove name="PHP-5.5-7i24.com" />
- <remove name="PHP-5.4-7i24.com" />
- <remove name="PHP-5.3-7i24.com" />
- <remove name="PHP-5.2-7i24.com" />
- <add name="PHP-5.4-7i24.com" path=".php" verb=""modules="FastCgiModule"scriptProcessor="c:php.4php-cgi.exe"resourceType="Either" />
- </handlers>
- <rewrite>
- <rules>
- <rule name="Imported Rule 1" enabled="true" 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>
[Nginx 服务器]
在原有的 nginx 重写文件里新增以下代码片段:
- location / {
- if (!-e $request_filename) {
- rewrite ^(.*)$ /index.php?s=/$1 last;
- break;
- }
- }
[apache 服务器]
易优 CMS 在 apache 服务器环境默认自动隐藏 index.php 入口.
如果发现没隐藏, 可以检查根目录. htaccess 是否含有以下代码段:
- <IfModule mod_rewrite.c>
- Options +FollowSymlinks
- RewriteEngine On
- RewriteCond %{REQUEST_FILENAME} !-d
- RewriteCond %{REQUEST_FILENAME} !-f
- RewriteRule ^(.*)$ index.php [L,QSA,E=PATH_INFO:$1]
- </IfModule>
如果存在, 继续查看 apache 是否开启了 URL 重写模块 rewrite_module , 然后重启服务就行了.
来源: http://www.bubuko.com/infodetail-2661028.html