Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.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 mod_重写和重定向导致循环_.htaccess_Mod Rewrite_Redirect - Fatal编程技术网

.htaccess mod_重写和重定向导致循环

.htaccess mod_重写和重定向导致循环,.htaccess,mod-rewrite,redirect,.htaccess,Mod Rewrite,Redirect,我在尝试重定向和重写时遇到问题。 我有一个站点example.com/show_table.php?table=12(最多99个表)。我想要漂亮的链接,所以我得到了这个。htacces rw规则: RewriteRule^table/([0-9]{1,2})$show_table.php?table=$1[L,NC] 现在有一些链接类似于example.com/table/12——绝对没问题。但我希望所有旧的链接重定向到新的格式。所以我使用重定向301,我添加到.htaccess这个代码: Re

我在尝试重定向和重写时遇到问题。 我有一个站点example.com/show_table.php?table=12(最多99个表)。我想要漂亮的链接,所以我得到了这个。htacces rw规则:

RewriteRule^table/([0-9]{1,2})$show_table.php?table=$1[L,NC]

现在有一些链接类似于example.com/table/12——绝对没问题。但我希望所有旧的链接重定向到新的格式。所以我使用重定向301,我添加到.htaccess这个代码:

RewriteCond%{REQUEST_URI}show_table.php
RewriteCond%{QUERY_STRING}^table=([0-9]{1,2})$
重写规则^show_table\.php$http://example.com/table/%1? [L,R=301,NC]

但是当我访问example.com/show_table.php?table=12时,我只收到了redir循环。我不明白-第一个是重写,第二个是重定向,没有两个重定向。你看到什么错误了吗


谢谢

您需要签入
请求
(包含完整的原始HTTP请求,如
GET/show_table.php HTTP/1.1
)而不是在条件中检查
REQUEST_URI
)。当Apache执行重写时,它会将请求URI更改为重写后的值,并将您发送到一个循环中

# Match show_table.php in the input request
RewriteCond %{THE_REQUEST} /show_table\.php
RewriteCond %{QUERY_STRING} ^table=([0-9]{1,2})$
# Do a full redirection to the new URL
RewriteRule ^show_table\.php$ http://example.com/table/%1? [L,R=301,NC]

# Then apply the internal rewrite as you already have working
RewriteRule ^table/([0-9]{1,2})$ show_table.php?table=$1 [L,NC]
您可以在
%{the_REQUEST}
条件中得到更具体的结果,但是使用
show_table\.php
作为表达式应该是足够的,并且不会有什么害处

您需要仔细阅读请求中的注释

注意:从技术上讲,您可以在相同的
RewriteCond
中捕获查询字符串,并将其简化为一个条件。这个稍微短一点:

# THE_REQUEST will include the query string so you can get it here.
RewriteCond %{THE_REQUEST} /show_table\.php\?table=([0-9]{1,2})
RewriteRule ^show_table\.php$ http://example.com/table/%1? [L,R=301,NC]

您需要在请求重写秒中转义
\?
regex@AnetteD. 很乐意帮忙。您是使用两个
RewriteCond
版本还是组合版本?我能问一下“完全重定向”的问题吗?是否也可以“小重定向”?因为我想在本地主机和网站上使用相同的.htaccess,但如果我将“/table/%1”或“/table/%1”放入redir规则,我将收到“”而不是“@anet”。我将不得不考虑一段时间来想出一个解决方案。我自己-我将在Apache中创建一个虚拟主机,这样开发环境也位于文档根
localhost/table/12
而不是位于子目录
localhost/example\u com/table/12
中,这样开发就与生产非常相似。以这种方式尝试使用mod_重写来适应不同的重定向路径有点麻烦。