【问题标题】:.htaccess /controller/action pattern calling twice public/index.php.htaccess /controller/action 模式调用两次 public/index.php
【发布时间】:2020-04-06 19:10:28
【问题描述】:

晚上好,

我已尽力在标题中描述该问题,并将在此处进一步详细说明:

我有一个使用此 URI 模式 /controller/action/parameters 的应用程序(即:register/complete/email:token:any_other_parameter)(在我的情况下,参数被分解),只要我只传递控制器URI(即:/register)它工作正常,我站在公共文件夹 /public/index.php 中的索引不会被调用两次。

但是,当为控制器 /register/complete 或 /admin/login(例如)指定操作时,public/index.php 会被调用两次。

这是我在应用根目录 covoit/ 和 covoit/public 文件夹中的 .htaccess。

根 .htaccess :

RewriteEngine On
RewriteBase /covoitudiant/

RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^%1 [L,NE,R=301]

RewriteRule ^/?([a-zA-Z]+)?/?([a-zA-Z]+)?/?(.*)? public/index.php?controller=$1&action=$2&parameters=$3 [L,QSA]

ErrorDocument 404 /covoitudiant/error

/public/.htaccess

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews -Indexes
    </IfModule>

    RewriteEngine On

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

我不太明白自己解决这个问题的概念,关于为什么从 URI 控制器/动作或控制器/动作/参数接收时 index.php 被调用两次...

【问题讨论】:

    标签: .htaccess redirect controller action public


    【解决方案1】:

    问题已解决。

    一个丢失的文件再次调用了我的 Apache 规则,因此我的控制器被调用了两次。

    这是由于路径错误的音频文件丢失。

    另外,我使用 Apache 的 vhost 来创建一个 .htaccess 文件,并根据我的迷你框架重新调整了规则。

    如果这也可以帮助其他人,结果如下:

    <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteBase /
    
        # Handle Authorization Header.
        RewriteCond %{HTTP:Authorization} .
        RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
    
        # Handle Trailing Slashes.
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_URI} (.+)/$
        RewriteRule ^ %1 [L,R=301]
    
        # If not Folder or File.
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_URI} !^index.php
        RewriteRule ^/?([a-zA-Z]+)?/?([a-zA-Z]+)?/?(.*)? index.php?controller=$1&action=$2&parameters=$3 [L,QSA]
    </IfModule>
    

    祝你有美好的一天!

    【讨论】:

      猜你喜欢
      • 2013-01-08
      • 1970-01-01
      • 2016-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-05
      相关资源
      最近更新 更多