要去除URL中的index.php和查询字符串,可以使用Apache的URL重写功能来实现。以下是一个示例的解决方法:
确保你的服务器已经安装了Apache和mod_rewrite模块。
在你的网站根目录下创建一个名为.htaccess
的文件。如果已存在,请确保它没有其他内容。
在.htaccess
文件中添加以下代码:
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/index\.php [NC]
RewriteRule ^(.*)index\.php$ /$1 [R=301,L]
RewriteCond %{QUERY_STRING} !=""
RewriteRule ^(.*)$ /$1? [R=301,L]
这些代码将按照以下规则进行重写:
第一个RewriteCond
和RewriteRule
的组合将重定向带有index.php
的URL到没有index.php
的URL。例如,http://example.com/index.php
将被重定向到http://example.com/
。
第二个RewriteCond
和RewriteRule
的组合将重定向带有查询字符串的URL到没有查询字符串的URL。例如,http://example.com/page?param=value
将被重定向到http://example.com/page
。
请注意,这些重写规则将会进行永久重定向(301)。如果你想要临时重定向(302),将R=301
替换为R=302
。
.htaccess
文件并上传到你的网站根目录。现在,当访问带有index.php
和查询字符串的URL时,它们将被重写为没有index.php
和查询字符串的URL。