【问题标题】:Apache, rewrite/redirect all to index.php - is not empty REQUESTED_FILENAMEApache,全部重写/重定向到 index.php - 不为空 REQUESTED_FILENAME
【发布时间】:2017-01-04 16:52:51
【问题描述】:

我想将所有内容转发到 index.php,除非没有指定文件 (REQUESTED_FILENAME) 或换句话说 URI 为空或取一个 /

我已经尝试过这种条件来重写/重定向到.htaccess - 文件中的 index.php。

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php/$1 [QSA]

如果我调用http://my.dom/test.php,将显示test.php 的内容(!-f)。

下一次尝试:检查,如果请求的文件不是 index.php,然后重定向到 index.php

RewriteCond %{REQUEST_FILENAME} !index.php
RewriteRule ^(.*)$ /index.php/$1 [QSA,R=301]

好的,这行得通。但我希望 no 通过空请求的文件重定向。

下一次尝试:如果请求的文件不为空且请求的文件不是 index.php,则重定向

RewriteCond %{REQUEST_FILENAME} !^$
RewriteCond %{REQUEST_FILENAME} !index.php
RewriteRule ^(.*)$ /index.php/$1 [QSA,R=301]

如果我打电话给http://my.dom/test.php 它工作正常,重定向到 index.php/test.php :D。

如果我调用http://my.domhttp://my.dom/,它也会重定向到 index.php。 但我希望 no 通过空请求的文件重定向。

更新(1) 我现在已经在我的 .htaccess 中了。

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} -f 
RewriteRule ^(.*)$ / [QSA,R=301]
#this redirect all files to root

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ /index.php/$1 [QSA,L]
#this rewrite on server side to index.php

这不起作用。 http://my.dom/test.php 被重定向到 http://my.dom/。但是第二条规则会产生内部(550)错误(每次重定向)。我在第一部分设置了一个新条件

RewriteCond %{REQUEST_FILENAME} -f 
RewriteCond %{REQUEST_URI} !^/.*$
RewriteRule ^(.*)$ / [QSA,R=301]
#this redirect all files to root

由此:http://my.dom/test.php 不会重定向。


请告诉我我的错误在哪里。也许还有一些解决方案。

问候

【问题讨论】:

  • 这样您就知道哪里出了问题并帮助您理解,您几乎已经完成了,但您需要使用REQUEST_URI 来满足您在第一句话中所说的两种情况。此外,为了防止重定向,您需要删除 301 重定向,它是 [QSA,L],这应该是 RewriteCond %{REQUEST_URI} !^/$
  • 重定向没问题。如果 URL my.dom/file.php - 那么它应该被重定向到 my.dom 而不是重写。通过重写,客户端的 URL 不会改变。我已经更新了我的尝试。

标签: apache .htaccess redirect mod-rewrite


【解决方案1】:

因为/不是文件,/指向你的根目录。您无法使用请求文件名变量匹配您的根目录。要将您的主页从规则中排除,只需将您的正则表达式模式更改为 (.+)

或者你可以使用下面的规则

RewriteEngine on

RewriteRule ((?!index\.php).+) /index.php/$1 [L]

【讨论】:

    猜你喜欢
    • 2020-05-02
    • 1970-01-01
    • 1970-01-01
    • 2017-04-05
    • 1970-01-01
    • 1970-01-01
    • 2014-08-31
    • 2014-12-15
    • 2018-04-03
    相关资源
    最近更新 更多