且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

htaccess合并2个带有下划线的句段

更新时间:2023-02-26 11:12:10


但是访问 http://example.com/main/id_id 重定向到 http://example.com/main/id/id 。我想知道为什么为什么不使用 R 进行重定向。

But accessing http://example.com/main/id_id just redirects to http://example.com/main/id/id. I'm wondering why it is redirecting since I didn't use R flag.

如果您在 RewriteRule substitiution 中指定了绝对URL(即方案+主机名),则Apache将隐式触发外部(302)重定向,无论是否显式包括 R 标志。

If you specify an absolute URL (ie. scheme + Hostname) in the RewriteRule substitiution then Apache will implicitly trigger an external (302) redirect, regardless of whether you've explicitly included the R flag or not.


RewriteRule ^main/id_id$ /main/id/id/ [L]

应该可以,但是不行。

WordPress会根据初始请求URL(即$ $ _ SERVER ['REQUEST_URI'] PHP超全局性的b $ b值),而不是重写的URL。所以,是的,这是一个 WordPress问题。您将需要为此URL创建一个附加的路由(而不是重写它)。但是,这可能会导致内容重复等问题。

WordPress routes the URL based on the initial request URL (ie. the value of the $_SERVER['REQUEST_URI'] PHP superglobal), not the rewritten URL. So, yes, this is a "WordPress thing". You would need to create an additional "route" for this URL (not rewrite it). However, this potentially creates issues with duplicate content etc.

但是,如果您要更改现有的URL,该URL可能已经链接到搜索引擎并由搜索引擎建立了索引,那么可以说这应该是301外部重定向而不是内部重写。

However, if you are changing an existing URL that has perhaps been linked to and indexed by search engines then this arguably should be a 301 external redirect and not an internal rewrite.


RewriteBase /main/
RewriteCond %{REQUEST_URI} ^/main/id_id(.*)$
RewriteRule .* http://example.com/main/id/id%1 [L]


这可以简化为单个指令:

This can be simplified to a single directive:

RewriteRule ^main/id_id(.*) /main/id/id$1 [R,L]

RewriteBase 指令在这里没有执行任何操作。 RewriteBase 指令仅声明用于相对路径替换的URL路径。

The RewriteBase directive is not doing anything here. The RewriteBase directive simply states the URL-path that should be used for relative path substitutions.