简体   繁体   中英

Apache2 rewrite rule to remove index.php from URL (Slim Framework)

I have a project in slim framework where my folder structure is like this:

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

Now, my problem is that I want to remove index.php from my API URLs so that I can have something like http://127.0.0.1/project/api/test instead of http://127.0.0.1/project/api/index.php/test . My .htaccess looks like this but doesn't work: RewriteEngine On

# 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]

This should work for you.

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

Use this in your Root/.htaccess file

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

Thanks guys but this worked for me:

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

Hope it helps someone else too...

I performed the following steps to make it work on Ubuntu standalone server.

Step 1 - you have to enable rewrite mod

a2enmod rewrite

Step 2 - make sure your apache settings in /etc/apache2/apache2.conf as follow

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

Make sure All Override should be all

Step 3 - Create .htaccess file with following

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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