简体   繁体   English

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

[英].htaccess redirect to index.php in any directory

i have been working on a framework that i am creating in php and the essential part for it to work is to send all the url queries to index.php in the root folder. 我一直在研究我在php中创建的框架,要使其正常工作,必不可少的部分是将所有url查询发送到根文件夹中的index.php。 I managed to do it in the folloowing code at my .htaccess file 我设法在.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

The problem occurs when i move the site to a different folder for example 例如,当我将网站移至其他文件夹时,就会出现问题

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

and it stops working here. 它在这里停止工作。 The same happens if i move the framework application to a sub-domain as well. 如果将框架应用程序也移到子域,也会发生相同的情况。

so I am trying to modify .htaccess to rewrite to index.php properly where the .htaccess file is at regardless if it is in a sub-directory or sub-domain. 因此,我试图修改.htaccess以便将.htaccess文件所在的位置正确地重写为index.php,无论它位于子目录还是子域中。 How can i get the .htaccess to know that application is in a sub-directory and rewrite to it properly so change of location does not break .htaccess pointing to the right file? 我怎样才能让.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

As you can see it needs to send the requests to index.php where htaccess is also located at. 如您所见,它需要将请求发送到htaccess所在的index.php。

Edit based on new examples: 根据新示例进行编辑:

How about something like this? 这样的事情怎么样? Basically check to make sure it's not a file, If not a file redirect to index.php 基本上检查以确保它不是文件,如果不是文件,则重定向到index.php

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond $1 !^(index\.php)
RewriteRule ^(.+)$ index.php [PT,L,QSA]

You can use this trick to always write in index.php of the current directory: 您可以使用此技巧始终写入当前目录的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]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM