.htaccess htaccess重定向或重写URL-向URL添加一个段,而不会导致无限循环
我有一个URL,我需要添加一个段.htaccess htaccess重定向或重写URL-向URL添加一个段,而不会导致无限循环,.htaccess,url,redirect,mod-rewrite,url-rewriting,.htaccess,Url,Redirect,Mod Rewrite,Url Rewriting,我有一个URL,我需要添加一个段/#/到 这不起作用,因为我有一个循环 Options +FollowSymLinks RewriteEngine On RedirectMatch "/constantsegment/(.*)" "/constantsegment/#!/$1" 例如,假设我的URL是http://example.com/constantsegment/changeablesegment。我需要网站重定向到http://example.c
/#/代码>到
这不起作用,因为我有一个循环
Options +FollowSymLinks
RewriteEngine On
RedirectMatch "/constantsegment/(.*)" "/constantsegment/#!/$1"
例如,假设我的URL是http://example.com/constantsegment/changeablesegment
。我需要网站重定向到http://example.com/constantsegment/#!/可变段
请注意,有一个名为#的URL段(文件夹)代码>介于constantsegment
和changeablesegment
之间
我遇到的问题是,在尝试附加/#时创建了重定向循环/编码>到/constantsegment/
。我怎样才能添加/#/代码>到末尾,然后添加所有其他段
再次
应该重定向到
http://example.com/constantsegment/#!/changeablesegment
http://example.com/products/#!/cars/blue
另一个示例(在本例中,产品是常量段)
应该重定向到
http://example.com/constantsegment/#!/changeablesegment
http://example.com/products/#!/cars/blue
只需将*
更改为+
(1个或多个),以避免匹配目标URL并导致重定向循环。因为片段标识符不会在重定向请求上传递回服务器
您还可以通过捕获第一个“常量”路径段来避免重复。例如:
RedirectMatch "/(constantsegment)/(.+)" "/$1/#!/$2"
Options FollowSymLinks
RewriteEngine On
RewriteRule ^(constantsegment)/(.+) /$1/#!/$1 [NE,R,L]
请注意,上述mod_alias指令(即RedirectMatch
)与mod_rewrite(即RewriteEngine On
)无关。这里也不需要选项和语法链接。除非您在.htaccess
文件的其他地方使用mod_rewrite
如果您已经在为其他重定向使用mod_rewrite,那么您可能应该改用mod_rewrite(即RewriteRule
),以避免意外冲突。例如:
RedirectMatch "/(constantsegment)/(.+)" "/$1/#!/$2"
Options FollowSymLinks
RewriteEngine On
RewriteRule ^(constantsegment)/(.+) /$1/#!/$1 [NE,R,L]
需要使用NE
标志来防止#
(哈希)被URL编码并被视为URL路径的一部分
请注意,所有这些重定向都是302(临时)。这是一个很好的解释,对新手来说非常好。谢谢分享,干杯。