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
?/$1
RewriteRule
例如,从这个:
RewriteRule (.*) index.php?/$1 [L]
对此:
RewriteRule . index.php [L]
- 1 回答
- 0 关注
- 109 浏览
添加回答
举报