mod_rewrite的是忽略子目录中的规则的是、规则、目录中、mod_rewrite

2023-09-02 20:40:34 作者:温山软水

求购重写的行为(内部重写!)

http://<subdomain>.domain.tld/<path> -> /<subdomain>/<path>
http://www.domain.tld/path/file.php  -> /www/path/file.php
http://project.domain-tld/index.php  -> /project/index.php

文件夹结构:

/                        root
    .htaccess
    /www                 www.domain.tld
        index.php
        /www
            file.php
        /foo
            /bar
                file.php
    /project             project.domain.tld
        index.php
        someRandomFiles
    /somesubdomain       somesubdomain.domain.tld
        index.php
        someRandomFiles
    /anothersubdomain    anothersubdomain.domain.tld
         index.php
         someRandomFiles

完整的.htaccess

# Unicode
AddDefaultCharset utf-8

# Activate mod_rewrite
RewriteEngine on
RewriteBase /

# Subdomains
#  Extract (required) subdomain (%1), and first path element (%3), discard port number if present (%2)
RewriteCond %{HTTP_HOST}<>%{REQUEST_URI} ^([^.]+).janbuschtoens.de(:80)?<>/([^/]*) [NC]
#  Rewrite only when subdomain not equal to first path element (prevents mod_rewrite recursion)
RewriteCond %1<>%3 !^(.*)<>1$ [NC]
#  Rewrite to /subdomain/path
RewriteRule ^(.*) /%1/$1 [L]

我的的.htaccess 似乎工作。你可以住考验在这里:

My .htaccess seems to work. You can live test it here:

http://test.janbuschtoens.de/ 改写为 /测试/ http://www.janbuschtoens.de/ 改写为 / WWW / http://test.janbuschtoens.de/ rewrites to /test/ http://www.janbuschtoens.de/ rewrites to /www/

但有一些奇怪的行为在子目录中。 的mod_rewrite 似乎忽视了规则,如果在请求路径的第一个目录同名的子域本身。例如:

But there is some strange behaviour in subdirectories. mod_rewrite seems to ignore the rule if the first directory in the requested path has the same name as the subdomain itself. For example:

的http://www.domain.tld/foo/bar/file.php - &GT; /www/foo/bar/file.php - 精细 的http://www.domain.tld/ - &GT; / WWW / - 精细 的http://www.domain.tld/www/ - &GT; / WWW / - 应该是:/网络/网络/ 的http://www.domain.tld/www/www/ - &GT; /网络/网络/ - 应该是:/网络/网络/网络/ http://www.domain.tld/foo/bar/file.php -> /www/foo/bar/file.php - Fine! http://www.domain.tld/ -> /www/ - Fine! http://www.domain.tld/www/ -> /www/ - Should be: /www/www/ http://www.domain.tld/www/www/ -> /www/www/ - Should be: /www/www/www/

有关另一个现场测试:

http://test.janbuschtoens.de/ 改写为 /测试/ http://test.janbuschtoens.de/test/ 改写为 /测试/ http://test.janbuschtoens.de/ rewrites to /test/ https://m.xsw88.com/allimgs/daicuo/20230902/52.png 被适当地重定向到 /www/www/123.png ,但随后进入下一个循环,在那里得到的重定向到 /www/www/www/123.png 然后周而复始。

This is the only good rule that I was able come up with, otherwise after initial rewriting (which is very easy) it goes into the loop (and that is the problem). For example: www.domain.com/www/123.png gets properly redirected into /www/www/123.png, but then goes to the next loop, where it get's redirected to /www/www/www/123.png and then again and again.

此规则的仅如果最终文件名的被调用的确存在

This rule ONLY gets invoked if FINAL filename DOES EXIST.

RewriteCond %{HTTP_HOST} ^(.+).domain.com$
RewriteCond %{DOCUMENT_ROOT}/%1/$1 -f [OR]
RewriteCond %{DOCUMENT_ROOT}/%1/$1 -d
RewriteRule ^(.*)$ /%1/$1 [QSA,L]

例如:如果你要求 www.domain.com/www/123.png ,以及文件/文件夹 WEBSITEROOT /网络/网络/ 123.png 存在,那么它将被重写,否则什么都没有。

For example: if you request www.domain.com/www/123.png, and file/folder WEBSITEROOT/www/www/123.png exist, then it will be rewritten, otherwise nothing.

同样在这里:如果你要求 meow.domain.com / ..但没有 WEBSITEROOT /喵/ 您的驱动器上的文件夹,它就会一事无成。

The same here: if you request meow.domain.com/ .. but have no WEBSITEROOT/meow/ folder on your drive, it gets nowhere.

请注意,这仍然没有多大帮助,如果您有子文件夹的名称相同的子域。例如:如果你要求 www.domain.com 就应该改写为 WEBSITEROOT / WWW / ...但如果你也有 WEBSITEROOT /网络/网络/ 则(的循环,因为)它会被改写成 WEBSITEROOT /网络/网络/ 代替。

Please note, this still will not help much if you have subfolder with the same name as subdomain. For example: if you request www.domain.com it should be rewritten to WEBSITEROOT/www/ ... but if you also have WEBSITEROOT/www/www/ then (because of loop) it will be rewritten to WEBSITEROOT/www/www/ instead.

不幸的是我还没有找到办法如何绕过它。如果你想 - 你可以试着与我结合你的规则

Unfortunately I have not found the way how to bypass it. If you wish -- you can try combining your rules with mine.