Apache24のmod_rewrite.cを用いてURLをリネームしたいのですが、なかなか想定したように動いてくれません。
やりたいことは、CGIに対するアクセス(*.py)の拡張子を隠したいです。
例えば ./a/b/yyy のURLにをクリックすると ./a/b/yyy.py にアクセスしてほしいです。
色々調べて以下のようなルールにしたのですが、この場合だと ./aaa/bbb/ccc/ にアクセスした場合、本来なら ./aaa/bbb/ccc/index.html にアクセスしてほしいが./aaa/bbb/ccc/.pyになり、404が表示されてしまいます。
また、./aaa/file (存在するファイル) にアクセスしようとすると ./aaa/file.py になり、こちらも404になってしまいます。
実装したいルールとして、可能であれば .pyを付けた場合のファイルが見つかれば .pyにリネーム、存在しなければ通常通りの動作を行う、といったものにしたいです。
これが難しければ、現在のルール + and条件としてURLの最後が/の場合は変換を行わないというものにしたいです。
どの様にすればいいか教えていただけると幸いです。
実行環境はwindows11、アクセスはlocalhostでのみテストしています。
log
1[Thu Jan 20 16:31:13.005260 2022] [authz_core:debug] [pid 22352:tid 1084] mod_authz_core.c(815): [client ::1:54067] AH01626: authorization result of Require all granted: granted, referer: http://localhost/ 2[Thu Jan 20 16:31:13.005260 2022] [authz_core:debug] [pid 22352:tid 1084] mod_authz_core.c(815): [client ::1:54067] AH01626: authorization result of <RequireAny>: granted, referer: http://localhost/ 3[Thu Jan 20 16:34:43.075744 2022] [rewrite:trace2] [pid 22352:tid 1076] mod_rewrite.c(480): [client ::1:53921] ::1 - - [localhost/sid#1d9f9d09c98][rid#1d9fb0cf1b0/initial] init rewrite engine with requested uri /file, referer: http://localhost/ 4[Thu Jan 20 16:34:43.075744 2022] [rewrite:trace3] [pid 22352:tid 1076] mod_rewrite.c(480): [client ::1:53921] ::1 - - [localhost/sid#1d9f9d09c98][rid#1d9fb0cf1b0/initial] applying pattern '^([^\\.]+)$' to uri '/file', referer: http://localhost/ 5[Thu Jan 20 16:34:43.075744 2022] [rewrite:trace4] [pid 22352:tid 1076] mod_rewrite.c(480): [client ::1:53921] ::1 - - [localhost/sid#1d9f9d09c98][rid#1d9fb0cf1b0/initial] RewriteCond: input='/file' pattern='!-f' => matched, referer: http://localhost/ 6[Thu Jan 20 16:34:43.075744 2022] [rewrite:trace4] [pid 22352:tid 1076] mod_rewrite.c(480): [client ::1:53921] ::1 - - [localhost/sid#1d9f9d09c98][rid#1d9fb0cf1b0/initial] RewriteCond: input='/file' pattern='!-d' => matched, referer: http://localhost/ 7[Thu Jan 20 16:34:43.075744 2022] [rewrite:trace2] [pid 22352:tid 1076] mod_rewrite.c(480): [client ::1:53921] ::1 - - [localhost/sid#1d9f9d09c98][rid#1d9fb0cf1b0/initial] rewrite '/file' -> '/file.py', referer: http://localhost/ 8[Thu Jan 20 16:34:43.075744 2022] [rewrite:trace2] [pid 22352:tid 1076] mod_rewrite.c(480): [client ::1:53921] ::1 - - [localhost/sid#1d9f9d09c98][rid#1d9fb0cf1b0/initial] local path result: /file.py, referer: http://localhost/ 9[Thu Jan 20 16:34:43.075744 2022] [rewrite:trace2] [pid 22352:tid 1076] mod_rewrite.c(480): [client ::1:53921] ::1 - - [localhost/sid#1d9f9d09c98][rid#1d9fb0cf1b0/initial] prefixed with document_root to C:/Apache24/htdocs/file.py, referer: http://localhost/ 10[Thu Jan 20 16:34:43.075744 2022] [rewrite:trace1] [pid 22352:tid 1076] mod_rewrite.c(480): [client ::1:53921] ::1 - - [localhost/sid#1d9f9d09c98][rid#1d9fb0cf1b0/initial] go-ahead with C:/Apache24/htdocs/file.py [OK], referer: http://localhost/
httpd.conf
1<IfModule mod_rewrite.c> 2RewriteEngine On 3LogLevel debug rewrite:trace8 4RewriteCond %{REQUEST_FILENAME} !-f 5RewriteCond %{REQUEST_FILENAME} !-d 6#RewriteCond %{REQUEST_URI} !-U 7RewriteRule ^([^\.]+)$ $1.py [NC,L] 8</IfModule>

回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。