使用 .htaccess 和 mod_rewrite 重定向目录(有少数例外)

发布于 2024-09-03 20:24:32 字数 1027 浏览 0 评论 0原文

我在 ManagedFusion 设置文件(类似于 .htaccess)中使用 mod_rewrite,并且希望将所有传入请求转发到服务器上的另一个端口(少数文件夹除外)。

到目前为止,我已经有了这个(文件夹 ui、forms、clientsystem 和 widgets 被忽略)

RewriteEngine On  

RewriteCond %{REQUEST_URI} !ui$
RewriteCond %{REQUEST_URI} !forms$
RewriteCond %{REQUEST_URI} !clientsystem$
RewriteCond %{REQUEST_URI} !widgets$
RewriteRule ^/(.*) http://localhost:8050/$1 [P,L]

只要这些异常目录不存在,它就可以正常工作,但是当它们被创建时,重写引擎将再次触发,在这种情况下它将匹配规则,从而将我转发到另一个端口。

我该如何解决这个问题?

另一个问题,如果我也希望转发像“?file=bla”这样的查询,这会简单地像这样工作吗?

编辑:这就是我最终得到的结果:

RewriteCond %{QUERY_STRING} ^(.+)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond $1 !^ui
RewriteCond $1 !^forms
RewriteCond $1 !^clientsystem
RewriteCond $1 !^widgets
RewriteRule ^/?(.*) http://localhost:8050/$1?%1 [P,L,R]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond $1 !^ui
RewriteCond $1 !^forms
RewriteCond $1 !^clientsystem
RewriteCond $1 !^widgets
RewriteRule ^/?(.*) http://localhost:8050/$1 [P,R]

有些冗余,但它有效。

I'm using mod_rewrite in a ManagedFusion settings file (similar to .htaccess) and I want to forward all incoming requests to another port on the server, except for a few folders.

So far I have this (with the folder ui, forms, clientsystem and widgets being ignored)

RewriteEngine On  

RewriteCond %{REQUEST_URI} !ui$
RewriteCond %{REQUEST_URI} !forms$
RewriteCond %{REQUEST_URI} !clientsystem$
RewriteCond %{REQUEST_URI} !widgets$
RewriteRule ^/(.*) http://localhost:8050/$1 [P,L]

This works fine as long as those exception directories don't exist, however when they are created, the rewriteengine will simply trigger again and in that case it will match the rule, thereby forwarding me to the other port after all.

How could I solve this?

Another question, if I want queries like '?file=bla' to be forwarded as well, will this simply work like this?

Edit: this is what I ended up with:

RewriteCond %{QUERY_STRING} ^(.+)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond $1 !^ui
RewriteCond $1 !^forms
RewriteCond $1 !^clientsystem
RewriteCond $1 !^widgets
RewriteRule ^/?(.*) http://localhost:8050/$1?%1 [P,L,R]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond $1 !^ui
RewriteCond $1 !^forms
RewriteCond $1 !^clientsystem
RewriteCond $1 !^widgets
RewriteRule ^/?(.*) http://localhost:8050/$1 [P,R]

Some redundancy but it works.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱已欠费 2024-09-10 20:24:33

至于问题的第二部分,请将 RewriteRule 标志设置为:[P,L,QSA]。 QSA 代表查询字符串追加,并且会在重定向发生之前对最终字符串执行显而易见的操作。

顺便说一句,您很可能希望在标志中包含 R,因为它会真正将客户端重定向到新的 URL。

更新:所以现在我明白您无论如何都想跳过这些目录?

如果是这样,那么您可以使用

RewriteCond $1 !^ui
RewriteCond $1 !^forms
...
RewriteRule ^/(.*) http://localhost:8050/$1 [P,L,QSA]

它,在请求的 URL 以任何给定字符串开头的情况下,它不会匹配并且不会转发用户。

As for second part of your question, make RewriteRule flags to be like: [P,L,QSA]. QSA stands for Query String Append, and will do the obvious to your final string before redirection occurs.

By the way, you would most likely want to include R in flags, since it will truly redirect client to new URL.

Update: so now I understand that you want to skip those directories in any case?

If that's so, then you could use

RewriteCond $1 !^ui
RewriteCond $1 !^forms
...
RewriteRule ^/(.*) http://localhost:8050/$1 [P,L,QSA]

which would simply not match and not forward user in case requested URL starts with any of given strings.

裂开嘴轻声笑有多痛 2024-09-10 20:24:33

尝试添加此命令以仅在该名称的文件或目录不存在时应用规则:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

顺便说一句,我发现此 mod_rewrite 备忘单非常方便: http://dreev.es/modrewrite

Try adding this to only apply the rule if the file or directory by that name doesn't exist:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

Btw, I find this mod_rewrite cheat sheet pretty handy: http://dreev.es/modrewrite.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文