重写规则工作不正常?重写、不正常、规则、工作

2023-09-02 11:41:07 作者:奔月日嫦娥

我想实现一个前端控制器。所以我想请求重定向到index.php。这是我的.htaccess。

I am trying to implement a front controller. So I want to redirect requests to index.php. Here is my .htaccess.

RewriteEngine On

# Redirect everything to the Front Controller
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !^(css|images|files)/ index.php [NC,L]

目前,如果我访问以下网址,它的工作原理:

Currently, if I visit the following url, it works:

http://devserver/myapp/

不过,如果我访问的任何URL不解析到index.php,例如的http:// devserver / APP /博客,阿帕奇告诉我,它不能发现的index.php:

But, if I visit any url that does not resolve to index.php, for example http://devserver/app/blog, Apache tells me that it cannot find index.php:

所请求的网址    /absolute/path/to/myapp/index.php   在此服务器上找到。

The requested URL /absolute/path/to/myapp/index.php was not found on this server.

不过,这条道路确实存在我的服务器上,所以我认为正在发生的事情是Apache正在试图访问该网址就像一个将浏览器 - 而它,如果我输入不因为工作: /absolute/path/to/myapp/index.php 在我的浏览器,这是行不通的。于是,我决定在我的.htaccess中的最后一行改成这样:

But that path actually exists on my server, so I think what's happening is Apache is trying to access that url like a browser would - and it doesn't work because if I type in : /absolute/path/to/myapp/index.php in my browser, it doesn't work. So, I decided to change the last line in my .htaccess to this:

RewriteRule !^(css|images|files)/ http://myapp.com/index.php [NC,L]

但它仍然无法正常工作,因为现在当我检查 $ _ SERVER [REQUEST_URI] 在我的PHP code,它的的总是的解析为的index.php 因为Apache做了一个HTTP请求。​​

But it still doesn't work, because now when I check for $_SERVER[REQUEST_URI] in my PHP code, it always resolves to index.php because Apache made a HTTP request.

不管怎么说,能有人告诉我我在做什么错了,我有什么我可以做,使Apache的重定向一切的index.php?

Anyways, can someone please tell me what I'm doing wrong, and I what I can do to make Apache redirect everything to index.php?

谢谢

推荐答案

不知道,但试试这个。

RewriteEngine On

# Redirect everything to the Front Controller
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
# RewriteRule !^(css|images|files)/ index.php [NC,L]
RewriteRule ^(.*)$ index.php/$1 [L]