【问题标题】:Apache2 rewrite rule to remove index.php from URL (Slim Framework)Apache2 重写规则以从 URL 中删除 index.php(Slim 框架)
【发布时间】:2017-04-27 20:41:13
【问题描述】:

我在 slim 框架中有一个项目,我的文件夹结构是这样的:

root web directory
    --project
    -----api
    -----vendor
    -----index.php
    -----.htaccess

现在,我的问题是我想从我的 API URL 中删除 index.php,这样我就可以使用 http://127.0.0.1/project/api/test 而不是 http://127.0.0.1/project/api/index.php/test。我的 .htaccess 看起来像这样但不起作用: 重写引擎开启

# Some hosts may require you to use the `RewriteBase` directive.
# If you need to use the `RewriteBase` directive, it should be the
# absolute physical path to the directory that contains this htaccess file.
#
# RewriteBase /

Header add Access-Control-Allow-Origin "*"
Header add Access-Control-Allow-Headers "origin, x-requested-with, content-type"
Header add Access-Control-Allow-Methods "PUT, GET, POST, DELETE, OPTIONS"

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

【问题讨论】:

    标签: php apache .htaccess mod-rewrite


    【解决方案1】:

    这应该适合你。

    RewriteEngine On
    RewriteRule ^api/([^/]*)$ /project/api/index.php?value=$1 [L]
    

    【讨论】:

    • 介意解释一下吗?另外,查询Params会发生什么?
    【解决方案2】:

    在您的 Root/.htaccess 文件中使用它

       RewriteEngine on
       RewriteBase /
       RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^project/api/(.*)/?$ /project/api/index.php/$1 [QSA,NC,L]
    

    【讨论】:

      【解决方案3】:

      谢谢大家,但这对我有用:

      RewriteEngine On
      RewriteCond %{REQUEST_FILENAME} !-f 
      RewriteRule ^(.*)$ %{ENV:BASE}index.php [QSA,L]
      

      希望它也对其他人有所帮助...

      【讨论】:

        【解决方案4】:

        我执行了以下步骤以使其在 Ubuntu 独立服务器上运行。

        第 1 步 - 你必须启用重写模组

        a2enmod rewrite
        

        第 2 步 - 确保 /etc/apache2/apache2.conf 中的 apache 设置如下

        <Directory /var/www/>
            Options Indexes FollowSymLinks
            AllowOverride all
            Require all granted
        </Directory>
        

        确保 All Override 应为 all

        第 3 步 - 使用以下内容创建 .htaccess 文件

        RewriteEngine On
        RewriteCond %{REQUEST_FILENAME} -s [OR]
        RewriteCond %{REQUEST_FILENAME} -l [OR]
        RewriteCond %{REQUEST_FILENAME} -d
        RewriteRule ^.*$ - [NC,L]
        RewriteRule ^.*$ index.php [NC,L]
        

        【讨论】:

          猜你喜欢
          • 2012-12-22
          • 1970-01-01
          • 1970-01-01
          • 2017-07-22
          • 2018-11-08
          • 2012-12-30
          • 1970-01-01
          • 1970-01-01
          • 2014-10-10
          相关资源
          最近更新 更多