简体   繁体   English

htaccess 重定向或重写 URL - 向 URL 添加一个段而不会导致无限循环

[英]htaccess Redirect or Rewrite URL - adding a segment to the URL without causing an infinite loop

I have a URL that I need to add a segment /#!/ to.我有一个 URL,我需要向其中添加一个/#!/段。

This isn't working because I get a loop.这不起作用,因为我有一个循环。

Options +FollowSymLinks
RewriteEngine On
RedirectMatch "/constantsegment/(.*)" "/constantsegment/#!/$1"

For example, let's say my URL is http://example.com/constantsegment/changeablesegment .例如,假设我的 URL 是http://example.com/constantsegment/changeablesegment I need the site to redirect to http://example.com/constantsegment/#!/changeablesegment .我需要该站点重定向到http://example.com/constantsegment/#!/changeablesegment

Notice that there is a URL segment(folder) called #!请注意,有一个名为#! in between constantsegment and changeablesegment .constantsegmentchangeablesegment之间。

The problem I am running into is that a redirect loop is created when trying to append /#!/ to the /constantsegment/ .我遇到的问题是尝试将/#!/附加到/constantsegment/时会创建重定向循环。 How can I just added /#!/ to the end and then add all the other segment(s) after that.我怎么能在最后添加/#!/ ,然后在此之后添加所有其他段。

Again再次

http://example.com/constantsegment/changeablesegment

Should redirect to应该重定向到

http://example.com/constantsegment/#!/changeablesegment

Another example (in this case products is the constant segment)另一个例子(在这种情况下,产品是常量段)

http://example.com/products/cars/blue

Should redirect to应该重定向到

http://example.com/products/#!/cars/blue
RedirectMatch "/constantsegment/(.*)" "/constantsegment/#!/$1"

Simply change .* to .+ (1 or more) to avoid matching the target URL and causing a redirect loop.只需将.*更改为.+ (1 个或更多)以避免匹配目标 URL 并导致重定向循环。 Because the fragment identifier is not passed back to the server on the redirected request.因为片段标识符不会在重定向请求上传回服务器。

You can avoid repetition by capturing the first "constant" path segment as well.您也可以通过捕获第一个“恒定”路径段来避免重复。 For example:例如:

RedirectMatch "/(constantsegment)/(.+)" "/$1/#!/$2"

Note that the above mod_alias directive (ie. RedirectMatch ) has nothing to do with mod_rewrite (ie. RewriteEngine On ).请注意,上面的 mod_alias 指令(即RedirectMatch )与 mod_rewrite (即RewriteEngine On )无关。 And neither is Options FollowSymLinks required here.此处也不需要Options FollowSymLinks Unless you are using mod_rewrite elsewhere in your .htaccess file.除非您在.htaccess文件的其他地方使用 mod_rewrite。

If you are already using mod_rewrite for other redirects then you should probably use mod_rewrite (ie. RewriteRule ) instead (to avoid unexpected conflicts).如果您已经将 mod_rewrite 用于其他重定向,那么您可能应该改用 mod_rewrite (即RewriteRule )(以避免意外冲突)。 For example:例如:

Options FollowSymLinks
RewriteEngine On

RewriteRule ^(constantsegment)/(.+) /$1/#!/$1 [NE,R,L]

The NE flag is required to prevent the # (hash) being URL encoded and considered part of the URL-path.需要NE标志来防止# (哈希)被 URL 编码并被视为 URL 路径的一部分。

Note that all these redirects are 302 (temporary).请注意,所有这些重定向都是 302(临时)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM