htaccess合并2个带有下划线的句段 [英] htaccess merge 2 segments with underscore

查看:58
本文介绍了htaccess合并2个带有下划线的句段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用下划线将2个细分合并为1个。

I want to merge 2 segments into 1 using underscore.

示例:

http:// example .com / main / id_id / someurl

应指向

http://example.com/main/ id / id / someurl


Example:
http://example.com/main/id_id/someurl
should point to
http://example.com/main/id/id/someurl

据我所了解的htaccess内容:

What I got so far as I understand htaccess:

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

但是访问 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

还这么简单:

RewriteEngine On
RewriteBase /main/
RewriteRule ^main/id_id$ /main/id/id/ [L]

应该可以,但不能。任何帮助将不胜感激。

Should work, but it's not. Any help would be appreciated.

编辑:我认为这是一个WordPress的东西。我可以屏蔽直接访问图像之类的目录,但不能屏蔽页面

I think this is a wordpress thing. I can mask direct access to directories like images but I cannot mask pages

推荐答案


但是访问 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.

这篇关于htaccess合并2个带有下划线的句段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆