Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.htaccess htaccess重定向或重写URL-向URL添加一个段,而不会导致无限循环_.htaccess_Url_Redirect_Mod Rewrite_Url Rewriting - Fatal编程技术网

.htaccess htaccess重定向或重写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

我有一个URL,我需要添加一个段
/#/

这不起作用,因为我有一个循环

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(临时)。

这是一个很好的解释,对新手来说非常好。谢谢分享,干杯。