内部mod_rewrite导致无限重定向

时间:2014-09-12 12:17:25

标签: apache .htaccess mod-rewrite redirect ngrok

我在localhost:8080上有一台服务器,我通过一个名为ngrok的隧道软件向它发送了管道请求,所以现在 我想设置我的mod_rewrite策略来映射并提供mysubdomain.ngrok.comlocalhost:8080的任何请求,而无需重定向浏览器

# Turn on rewrites
RewriteEngine on
###
## Rules for "mysubdomain.ngrok.com"
###
RewriteCond %{HTTP_HOST} ^mysubdomain.ngrok.com$
RewriteRule .? %{REQUEST_URI} [L]
###
## Rules for "localhost:8080"
###
RewriteCond %{HTTP_HOST} ^localhost:8080$
RewriteRule . - [E=REWRITEBASE:/prestashop/]
RewriteRule ^api/?(.*)$ %{ENV:REWRITEBASE}webservice/dispatcher.php?url=$1 [QSA,L]

但我最终进入无限重定向循环! 避免这种情况和实现目标的最佳方法是什么?

[/initial] [perdir /Users/root/dev/bitnami-ps1/apps/prestashop/htdocs/] add path info postfix: /Users/root/dev/bitnami-ps1/apps/prestashop/htdocs/api -> /Users/root/dev/bitnami-ps1/apps/prestashop/htdocs/api/products
[/initial] [perdir /Users/root/dev/bitnami-ps1/apps/prestashop/htdocs/] strip per-dir prefix: /Users/root/dev/bitnami-ps1/apps/prestashop/htdocs/api/products -> api/products
[/initial] [perdir /Users/root/dev/bitnami-ps1/apps/prestashop/htdocs/] applying pattern '.?' to uri 'api/products'
[/initial] [perdir /Users/root/dev/bitnami-ps1/apps/prestashop/htdocs/] RewriteCond: input='mysubdomain.ngrok.com' pattern='^mysubdomain.ngrok.com$' => matched
[/initial] [perdir /Users/root/dev/bitnami-ps1/apps/prestashop/htdocs/] rewrite 'api/products' -> '/prestashop/api/products'
[/initial] [perdir /Users/root/dev/bitnami-ps1/apps/prestashop/htdocs/] internal redirect with /prestashop/api/products [INTERNAL REDIRECT]
[/initial/redir#1] [perdir /Users/root/dev/bitnami-ps1/apps/prestashop/htdocs/] add path info postfix: /Users/root/dev/bitnami-ps1/apps/prestashop/htdocs/api -> /Users/root/dev/bitnami-ps1/apps/prestashop/htdocs/api/products
[/initial/redir#1] [perdir /Users/root/dev/bitnami-ps1/apps/prestashop/htdocs/] strip per-dir prefix: /Users/root/dev/bitnami-ps1/apps/prestashop/htdocs/api/products -> api/products
[/initial/redir#1] [perdir /Users/root/dev/bitnami-ps1/apps/prestashop/htdocs/] applying pattern '.?' to uri 'api/products'
[/initial/redir#1] [perdir /Users/root/dev/bitnami-ps1/apps/prestashop/htdocs/] RewriteCond: input='mysubdomain.ngrok.com' pattern='^mysubdomain.ngrok.com$' => matched
[/initial/redir#1] [perdir /Users/root/dev/bitnami-ps1/apps/prestashop/htdocs/] rewrite 'api/products' -> '/prestashop/api/products'
[/initial/redir#1] [perdir /Users/root/dev/bitnami-ps1/apps/prestashop/htdocs/] internal redirect with /prestashop/api/products [INTERNAL REDIRECT]
...
[/initial/redir#10] [perdir /Users/root/dev/bitnami-ps1/apps/prestashop/htdocs/] add path info postfix: /Users/root/dev/bitnami-ps1/apps/prestashop/htdocs/api -> /Users/root/dev/bitnami-ps1/apps/prestashop/htdocs/api/products
[/initial/redir#10] [perdir /Users/root/dev/bitnami-ps1/apps/prestashop/htdocs/] strip per-dir prefix: /Users/root/dev/bitnami-ps1/apps/prestashop/htdocs/api/products -> api/products
[/initial/redir#10] [perdir /Users/root/dev/bitnami-ps1/apps/prestashop/htdocs/] applying pattern '.?' to uri 'api/products'
[/initial/redir#10] [perdir /Users/root/dev/bitnami-ps1/apps/prestashop/htdocs/] RewriteCond: input='mysubdomain.ngrok.com' pattern='^mysubdomain.ngrok.com$' => matched
[/initial/redir#10] [perdir /Users/root/dev/bitnami-ps1/apps/prestashop/htdocs/] rewrite 'api/products' -> '/prestashop/api/products'
[/initial/redir#10] [perdir /Users/root/dev/bitnami-ps1/apps/prestashop/htdocs/] internal redirect with /prestashop/api/products [INTERNAL REDIRECT]

1 个答案:

答案 0 :(得分:0)

查看日志和规则我可以​​看到此规则导致无限循环:

RewriteCond %{HTTP_HOST} ^mysubdomain.ngrok.com$
RewriteRule .? %{REQUEST_URI} [L]

因为它无条件地为domain = mysubdomain.ngrok.com

写一个URI给自己

只需将其注释掉,循环就会修复。