如何使用 .htaccess 从 URL 中删除 .cgi?

How To Remove .cgi From URL With .htaccess?

我在我的服务器上使用 .cgi 文件 运行 python。但是,每当有人访问该站点时,它会将他们带到 https://example.com/main.cgi/ (main.cgi is the name of the cgi file). Is there a way to remove this from the .htacess file, so it goes to https://example.com/ 但仍然是 运行 的 cgi 文件?

这是当前的 .cgi 文件:

RewriteEngine On
RewriteBase /website

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /main.cgi/ [L]

RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]

如何制作 RewriteRule 让 URL / 转到 /main.cgi/ 而不出现在 URL 中?

您应该将您的 https 实现作为您的 .htaccess 文件的第一条规则,请您尝试执行以下操作。根据您显示的示例编写,请确保在测试您的 URL 之前清除缓存。

RewriteEngine ON
##Rule for applying https to non-http calls.
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]

##Look for if any page doesn't have `/`slashes at the end then add it.
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^(.*)$ / [R=301,L]

##Rules for non exiting file or directory to main.cgi file.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ main.cgi [L]

注意: 使用 RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE] 个人在末尾给了我 1 个斜线,所以我写了一个单独的规则来添加尾随/ 对于这个,避免在 url.

末尾出现 2 次 /