.htaccess 如何重定向httpaccess

2cmtqfgy  于 2022-11-16  发布在  其他
关注(0)|答案(2)|浏览(112)

我试图重定向,但不工作,即使我把正确的代码从采取它从网络。

Redirect /index.php?page=8 /?page=8
vnjpjtjt

vnjpjtjt1#

Redirect /index.php?page=8 /?page=8

mod_alias Redirect指令与查询字符串不匹配,因此上述指令永远不会匹配,因此不执行任何操作。
要从可见URL中删除index.php(目录索引),您需要在.htaccess文件的顶部使用mod_rewrite。例如:

RewriteEngine On

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^index\.php$ / [R=301,L]

上面的代码会将/index.php?page=8的URL重定向到/?page=8。初始请求中的任何查询字符串都将直接传递到目标/替换。
检查REDIRECT_STATUS env var的 condition 确保我们不会得到由mod_dir(或Laravel前端控制器)将请求重写为index.php所导致的重定向循环。
清除您的浏览器缓存并首先使用302(临时)重定向进行测试。
但是,如果您只想将特定的URL /index.php?page=8(如问题中所述)重定向到/?page=8,则应编写如下规则:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,7}\s/index\.php?page=8\sHTTP
RewriteRule ^index\.php$ / [R=301,L]
pcrecxhr

pcrecxhr2#

你的htaccess代码应该是。

RewriteEngine On
RewriteRule ^index.php?page=$1 /?page=$1 [R=301,NC,L]

相关问题