简体   繁体   English

如何获取RewriteRule来重定向现有和不存在的页面?

[英]How to get RewriteRule to redirect both existing and non-existing pages?

Goal: Send all requests, regardless whether the directory/file exists or not, to http://example.com/index.php 目标:将所有请求(无论目录/文件是否存在)发送到http://example.com/index.php

What I have tried: 我试过的

Rewrite Engine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://example.com/index.php [L]

It redirects URL's of non-existing directories/files fine, but when I access an existing subdirectory and file http://example.com/test/test.php it only gives me the contents of the test.php page instead of the main index.php page. 它可以很好地重定向不存在的目录/文件的URL,但是当我访问现有的子目录和文件http://example.com/test/test.php ,它只为我提供了test.php页面的内容,而不是主要的index.php页面。

If I understand correctly, the RewriteCond %{REQUEST_FILENAME} !-f and RewriteCond %{REQUEST_FILENAME} !-d tell it to perform the RewriteRule if those criterias are met, which makes sense why it only redirects on non-existing directories and files. 如果我理解正确,则RewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-d告诉它如果满足那些条件,则执行RewriteRule ,这很有意义,为什么它仅在不存在的目录和文件上进行重定向。 But I've tried removing those RewriteCond 's, and all it does is put my site into a redirect loop. 但是我尝试删除了这些RewriteCond ,并且所做的只是将我的网站置于重定向循环中。

I've also tried doing 我也尝试过

Rewrite Engine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ http://example.com/index.php [L]

But then it doesn't redirect at all. 但是,它根本不会重定向。

What do I have to put to accomplish this? 我必须投入什么才能实现这一目标?

Your first example would only redirect things that does not exist to your index.php as the conditions you have there: 您的第一个示例只会将不存在的内容重定向到index.php因为您在此具有以下条件:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

Basically means if file or directory does not exist, do this. 基本上意味着如果文件或目录不存在,请执行此操作。

Your second example is even worst as it tries to see if a file, directory exist and does not exist which makes no sense. 您的第二个示例甚至更糟,因为它试图查看文件和目录是否存在,这没有任何意义。


To redirect anything existent or not to a main handler in this case index.php all you need is the below: 在这种情况下,要将任何存在或不存在的内容重定向到主处理程序index.php您需要做的是:

Options +FollowSymLinks -MultiViews

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_URI} !^/index.php$ [NC]
RewriteRule ^ index.php [L]

The condition makes sure you're not redirecting it to yourself to prevent a loop. 该条件确保您不会将其重定向到您自己以防止循环。 If you're running HTTPD version 2.4 and above you can simple use: 如果您正在运行HTTPD 2.4及更高版本,则可以简单地使用:

Options +FollowSymLinks -MultiViews

RewriteEngine On
RewriteBase /

RewriteRule ^ index.php [END]

The flag [END] takes care of not executing any further redirects. 标志[END]用于不执行任何进一步的重定向。

If you want to redirect any url, just use a RewriteRule , so it will redirect any non/existent file/directory to the RewriteRule rule 如果要重定向任何url,只需使用RewriteRule ,这样它将会将任何不存在/不存在的文件/目录重定向到RewriteRule规则

RewriteEngine on
RewriteBase /

RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]

RewriteRule ^(.*)$ index.php [L]

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

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