簡體   English   中英

通配符.htaccess並拒絕子域上的某些地址

[英]wildcard .htaccess and reject some address on subdomain

我希望up.example.com子域下存在的所有文件或目錄都被拒絕,並顯示404錯誤。

我也希望所有其他請求指向index.php

我寫這段代碼:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^up\.(.+)$ [NC]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ - [R=404,L,NS]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L,QSA]

當我為http://up.example.com/test.jpg測試此代碼時

如果根服務器上存在test.jpg文件,請顯示此錯誤:

Not Found
The requested URL /test.jpg was not found on this server.
Apache Server at up.example.com Port 80

沒關系!
但是當test.jpg不存在時,給我這個錯誤:

Not Found
The requested URL /index.php was not found on this server.
Apache Server at up.example.com Port 80

而是打開index.php (root上存在index.php

為什么? 以及如何解決?

試試這個

RewriteEngine On

RewriteCond %{HTTP_HOST} ^up\.(.+)$ [NC]  # remove if it is not related to subdomain
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L,QSA]

RewriteCond %{HTTP_HOST} ^up\.(.+)$ [NC]
RewriteCond %{REQUEST_URI} !^/index\.php
RewriteRule ^(.*)$ - [R=404,L,NS]

實際上,問題是您將所有內容都寫入了index.php並且在請求中變成了有效文件。 下次運行mod_rewrite時,第一個規則將其發送到404。

要克服此錯誤,請遵循以下規則:

RewriteEngine On

# if it is valid file or directory except for index.php
# then send 404 to browser
RewriteCond %{HTTP_HOST} ^up\. [NC]
RewriteCond %{THE_REQUEST} \s/+index\.php[?\s] [NC]
RewriteRule ^index\.php$ - [R=404,L,NC]

RewriteCond %{HTTP_HOST} ^up\. [NC]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule !^index\.php$ - [R=404,L,NC]

# send all non-file/directories to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM