gpt4 book ai didi

php - .htaccess 重定向到任何目录中的index.php

转载 作者:行者123 更新时间:2023-12-02 23:47:37 24 4
gpt4 key购买 nike

我一直在开发一个在 php 中创建的框架,其工作的基本部分是将所有 url 查询发送到根文件夹中的 index.php。 我设法在 .htaccess 文件中的以下代码中完成此操作

# Turn rewriting on
Options +FollowSymLinks
RewriteEngine On
# Redirect requests to index.php
#RewriteRule ^(phpmyadmin)($|/) - [L]
RewriteRule ^(html)($|/) - [L]
RewriteCond %{REQUEST_URI} !=/index.php
RewriteCond %{REQUEST_URI} !.*\.png$ [NC]
RewriteCond %{REQUEST_URI} !.*\.jpg$ [NC]
RewriteCond %{REQUEST_URI} !.*\.css$ [NC]
RewriteCond %{REQUEST_URI} !.*\.gif$ [NC]
RewriteCond %{REQUEST_URI} !.*\.js$ [NC]
RewriteRule .* /index.php

例如,当我将网站移动到其他文件夹时会出现问题

www.mydomain.com/subdir/{where the index.php is, along with htaccess}

它在这里停止工作。如果我也将框架应用程序移动到子域,也会发生同样的情况。

所以我尝试修改 .htaccess 以正确重写 .htaccess 文件所在的 index.php,无论它是否位于子目录或子域中。我怎样才能让 .htaccess 知道应用程序位于子目录中并正确重写它,以便更改位置不会破坏 .htaccess 指向正确的文件?

 www.domain.com/{.htaccess+index.php + framework} => works properly
www.subdom.domain.com/{.htaccess+index.php + framework} => does not work
www.domain.com/subdir/{.htaccess+index.php + framework} => does not work //send to www.domain.com/index.php
localhost/projectname/{.htaccess+index.php + framework} =>does not work

如您所见,它需要将请求发送到 htaccess 也位于的index.php。

最佳答案

你可以使用这个技巧来始终在当前目录的index.php中写入:

RewriteEngine On

# Generate BASE dynamically
# It compares REQUEST_URI variable (which is complete path) with the URI matched by
# RewriteRule (which is relative to current path) and gets differential in
$ %{ENV:BASE} variable.
RewriteCond %{REQUEST_URI}::$1 ^(.*?/)(.*)::\2$
RewriteRule ^(.*)$ - [E=BASE:%1]

RewriteRule ^(html)($|/) - [L]

# Redirect requests to %{ENV:BASE}index.php which is index.php of current directory
# It also makes sure index.php exists in current directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}%{ENV:BASE}index\.php -f [NC]
RewriteRule . %{ENV:BASE}index.php [L]

关于php - .htaccess 重定向到任何目录中的index.php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25574116/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com