如何将所有请求发送到 CGI 脚本而不出现在 .Htaccess 文件的 URL 中?
How To Send All Requests To A CGI Script Without It Appearing In The URL From The .Htaccess File?
我正在尝试将所有对我的网络服务器的请求发送到 .cgi
脚本,而不是它出现在 url 中(cgi 脚本是 运行 Python烧瓶)
例如,如果您转到此 url:
https://example.com/page/page2
它会将请求发送到路径 /page/page2
的 main.cgi
,但是 /main.cgi
不会出现在 url 中。另外,我希望这样用户可以 而不是 通过转到他们的 url 来访问任何静态文件。
到目前为止,我已经尝试了以下 .htaccess 文件:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /main.cgi/ [L]
遗憾的是,这允许用户通过转到 url 来访问静态文件。例如,url example.com/static/image.jpg
将 return 图像,而不是 return 从 cgi 脚本编辑的图像。
我也试过:
RewriteCond %{REQUEST_URI} !=/main.cgi
RewriteRule .*/main.cgi
但是,这会删除 url 路径。例如,这个 url: example.com/hello
将被重定向到 example.com
.
如何让 htaccess 向 cgi 脚本发送请求而不让它出现在 url 中?
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /main.cgi/ [L]
这会将所有未映射到真实文件(包括目录)的请求重写为 /main.cgi
,将请求的 URL-路径作为路径信息传递给您的脚本(您的 CGI 脚本大概是期待)。我假设这在这方面有效?
要重写 一切 那么您需要删除 RewriteCond
指令(正如您稍后所做的那样),但您确实需要阻止对 [=13] 的内部请求=] 正在内部重写,否则会创建一个重写循环(500 错误响应)。
例如:
RewriteRule !^main\.cgi main.cgi%{REQUEST_URI} [L]
这会将所有尚未针对 /main.cgi
的请求重写为 /main.cgi/<url>
。
RewriteRule
模式 上的 !
前缀运算符否定正则表达式 - 当正则表达式不匹配时规则成功。 RewriteRule
模式 在 .htaccess
中使用时没有斜杠前缀。此处不需要 substitution 字符串上的斜杠前缀。 REQUEST_URI
服务器变量包含相对于根的 URL 路径(包括斜杠前缀),因此不需要使用 RewriteRule
模式 .
RewriteCond %{REQUEST_URI} !=/main.cgi
RewriteRule .*/main.cgi
However, this removes the url path. For example, this url: example.com/hello would just be redirected to example.com.
这些指令实际上无效(RewriteRule
缺少第二个参数),所以实际上不能做任何事情吗?但是,RewriteRule
模式 .*/main.cgi
与您之前声明的 URL 不匹配,所以我还是看不出这是做任何事情。 (?)
我正在尝试将所有对我的网络服务器的请求发送到 .cgi
脚本,而不是它出现在 url 中(cgi 脚本是 运行 Python烧瓶)
例如,如果您转到此 url:
https://example.com/page/page2
它会将请求发送到路径 /page/page2
的 main.cgi
,但是 /main.cgi
不会出现在 url 中。另外,我希望这样用户可以 而不是 通过转到他们的 url 来访问任何静态文件。
到目前为止,我已经尝试了以下 .htaccess 文件:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /main.cgi/ [L]
遗憾的是,这允许用户通过转到 url 来访问静态文件。例如,url example.com/static/image.jpg
将 return 图像,而不是 return 从 cgi 脚本编辑的图像。
我也试过:
RewriteCond %{REQUEST_URI} !=/main.cgi
RewriteRule .*/main.cgi
但是,这会删除 url 路径。例如,这个 url: example.com/hello
将被重定向到 example.com
.
如何让 htaccess 向 cgi 脚本发送请求而不让它出现在 url 中?
RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ /main.cgi/ [L]
这会将所有未映射到真实文件(包括目录)的请求重写为 /main.cgi
,将请求的 URL-路径作为路径信息传递给您的脚本(您的 CGI 脚本大概是期待)。我假设这在这方面有效?
要重写 一切 那么您需要删除 RewriteCond
指令(正如您稍后所做的那样),但您确实需要阻止对 [=13] 的内部请求=] 正在内部重写,否则会创建一个重写循环(500 错误响应)。
例如:
RewriteRule !^main\.cgi main.cgi%{REQUEST_URI} [L]
这会将所有尚未针对 /main.cgi
的请求重写为 /main.cgi/<url>
。
RewriteRule
模式 上的 !
前缀运算符否定正则表达式 - 当正则表达式不匹配时规则成功。 RewriteRule
模式 在 .htaccess
中使用时没有斜杠前缀。此处不需要 substitution 字符串上的斜杠前缀。 REQUEST_URI
服务器变量包含相对于根的 URL 路径(包括斜杠前缀),因此不需要使用 RewriteRule
模式 .
RewriteCond %{REQUEST_URI} !=/main.cgi RewriteRule .*/main.cgi
However, this removes the url path. For example, this url: example.com/hello would just be redirected to example.com.
这些指令实际上无效(RewriteRule
缺少第二个参数),所以实际上不能做任何事情吗?但是,RewriteRule
模式 .*/main.cgi
与您之前声明的 URL 不匹配,所以我还是看不出这是做任何事情。 (?)