重定向目录使用的.htaccess和mod_rewrite的少数例外重定向、目录、mod_rewrite、htaccess

2023-09-02 20:36:26 作者:  ‘ 傻头-。 | ‘ 傻恼-。

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

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.

到目前为止,我有这个(与文件夹的用户界面,形式,clientsystem和小部件被忽略)

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]

这工作得很好,只要这些例外目录不存在,但在创建时,该RewriteEngine叙述只会再次触发,并在这种情况下,它会符合该规则,因此我转发到另一个端口毕竟。

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?

编辑:这是我结束了:

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.

推荐答案

至于你的问题的第二部分,使重写规则的标志是这样的: [P,L,QSA] 。 QSA代表查询字符串追加,和重定向发生前会做明显的到你最终的字符串。

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.

顺便说一句,你很可能要包含R标志,因为这将真正重定向客户端新网址。

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]

这将根本不匹配,如果请求的URL,不转发用户开始与任何特定的字符串。

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