Welcome to JiKe DevOps Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

mod rewrite - How To Remove .cgi From URL With .htaccess?

I use a .cgi file to run python on my server. However, whenever someone goes to the site, it brings them to 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/ but still runs the cgi file?

This is the current .cgi file:

RewriteEngine On
RewriteBase /website

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

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

How can I make a RewriteRule that makes the URL / go to /main.cgi/ without it appearing in the URL?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

Please log in or register to answer this question.

1 Answer

0 votes
by (71.8m points)

You should make your https implementation as a first rule of your .htaccess file, could you please try following. Written as per your shown samples, please make sure you clear your cache before testing your URLs.

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 ^(.*)$ $1/ [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]

NOTE: Using RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE] personally gives me 1 slash at end so I had written a separate rule for adding trailing / for this one, to avoid 2 times / at the end of url.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to JiKe DevOps Community for programmer and developer-Open, Learning and Share
...