为了账号安全,请及时绑定邮箱和手机立即绑定

从代码签名中删除和重定向索引.php

从代码签名中删除和重定向索引.php

PHP
慕桂英546537 2022-09-17 15:34:14
我有一个旧项目,它有一些奇怪的问题。其代码签名器项目(版本 2.X)。谷歌正在用索引索引我们的网址.php?。下面是一个示例https://www.website.com/index.php?/my-seo-friendly-uri我可以从上面的URL和另外2个变体中看到我的页面,如下所示https://www.website.com/index.php/my-seo-friendly-urihttps://www.website.com/my-seo-friendly-uri我们在整个网站中使用过。https://www.website.com/my-seo-friendly-uri我的问题是我如何重定向和到?https://www.website.com/index.php?/my-seo-friendly-urihttps://www.website.com/index.php/my-seo-friendly-urihttps://www.website.com/my-seo-friendly-uri我已经做过的事情<IfModule mod_rewrite.c>RewriteEngine OnRewriteRule ^index.php/(.*)$ /$1 [R=302,L]RewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-dRewriteRule ^(.*)$ index.php?/$1 [L] </IfModule>这会重定向到正常网址,但网址的版本仍未重定向index.phpindex.php?在我的 中,我有以下设置config.php$config['index_page'] = '';$config['uri_protocol'] = 'REQUEST_URI';
查看完整描述

1 回答

?
炎炎设计

TA贡献1808条经验 获得超4个赞

https://www.example.com/index.php?/my-seo-friendly-uri

此 URL 包含一个查询字符串,因此需要一个稍有不同的规则才能匹配它。该模式仅与 URL 路径匹配(在本例中)。查询字符串在其自己的变量中可用。RewriteRuleindex.php


在现有指令之前添加以下内容(除了匹配的指令 - 作为路径信息传递):/index.php/my-seo-friendly-url


# Redirect URLs of the form "/index.php?/my-seo-friendly-uri"

# And "/?/my-seo-friendly-uri"

RewriteCond %{ENV:REDIRECT_STATUS} ^$

RewriteCond %{QUERY_STRING} ^(/.*)

RewriteRule ^(index\.php)?$ %1 [QSD,R=302,L]

捕获查询字符串(第 2 个条件),并使用反向引用 () 来构造重定向。%1


为了防止重定向循环,需要针对环境变量进行检查的第一个条件,因为您似乎在稍后的重写中使用查询字符串方法来路由代码字符 URL。env var 在初始请求中为空,但在首次成功重写后设置为“200”(如 200 OK HTTP 状态)。REDIRECT_STATUSREDIRECT_STATUS


需要标志 (Apache 2.4+) 才能从重定向请求中丢弃原始查询字符串。如果您仍在使用 Apache 2.2,请改为将一个(空查询字符串)附加到替换字符串中。即。QSD?%1?


通过使匹配可选(即),它还将规范省略的URL,但仍包括查询字符串(当前可能是也可能不是问题)。例如。.index.php^(index\.php)?$index.php/?/my-seo-friendly-uri


请注意,这当前是 302(临时)重定向(与现有重定向一样)。只有在确认它工作正常后,才将其更改为301(永久)重定向,301由浏览器永久缓存,因此可能会使测试出现问题。


总结

您的文件应如下所示:.htaccess


RewriteEngine On


# Query string...

# Redirect URLs of the form "/index.php?/my-seo-friendly-uri"

# And "/?/my-seo-friendly-uri"

RewriteCond %{ENV:REDIRECT_STATUS} ^$

RewriteCond %{QUERY_STRING} ^(/.*)

RewriteRule ^(index\.php)?$ %1 [QSD,R=302,L]


# Path-Info...

# Redirect URLs of the form "/index.php/my-seo-friendly-uri"

# Also handles "/index.php" only

RewriteCond %{ENV:REDIRECT_STATUS} ^$

RewriteRule ^index.php(?:/(.*))?$ /$1 [R=302,L]


# CodeIgniter Front-controller

# (NB: Using query string method to pass the URL)

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule (.*) index.php?/$1 [L]

附加说明...

  • 包装器不是必需的。<IfModule>

  • (.*)与因为正则表达式在默认情况下是贪婪的一样。^(.*)$

  • 我已经修改了您现有的路径信息重定向(即)以仅重定向请求。这现在需要一个额外的条件来避免重定向循环。/index.php/foo/index.php

  • 您的 CodeIgniter 前端控制器正在使用查询字符串方法传递到后端(如问题中所用)。但是,您已经设置了 - 这与此相矛盾(尽管不一定是问题)。但是,如果您使用的是该方法,则可以从最终替换字符串的末尾删除部件。例如:/my-seo-friendly-url$config['uri_protocol'] = 'REQUEST_URI';REQUEST_URI?/$1RewriteRule

    例如,从这个:

    RewriteRule (.*) index.php?/$1 [L]

    对此:

    RewriteRule . index.php [L]


查看完整回答
反对 回复 2022-09-17
  • 1 回答
  • 0 关注
  • 109 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号