user14065257
user14065257

Reputation: 63

Routing by $_SERVER["REQUEST_URI"] only works for base url and returns 404 Not Found on pages

For routing i need to get url input by $_SERVER["REQUEST_URI"] which works for my base url e.g. http://xxx.xxx.xxx.xx/api/ can return /api/ but any request with more inputs http://xxx.xxx.xxx.xx/api/test or http://xxx.xxx.xxx.xx/api/posts/3 return error:

Not Found

The requested URL was not found on this server.
Apache/2.4.41 (Ubuntu) Server at xxx.xxx.xxx.xx Port 80

I'm using this .htaccess in main root /var/www/html/ also Module rewrite already enabled

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

Update: my simple php file as http://xxx.xxx.xxx.xx/api/index.php

<?php

var_dump($_SERVER['REQUEST_URI']);
die();

// header( "Access-Control-Allow-Origin: *" );

// header( "Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With" );

// header( "Content-Type: application/json; charset=UTF-8" );

// header( "Access-Control-Allow-Methods: GET, POST, PUT, DELETE" );

// header( "X-Requested-With: XMLHttpRequest" );

// header( "Cache-Control: no-cache, must-revalidate" );

// header( "Pragma: no-cache" );

// header( "Expires: Sat, 26 Jul 1997 05:00:00 GMT" );

also the /etc/apache2/sites-available/000-default.conf content

<VirtualHost *:80>
        # The ServerName directive sets the request scheme, hostname and port that
        # the server uses to identify itself. This is used when creating
        # redirection URLs. In the context of virtual hosts, the ServerName
        # specifies what hostname must appear in the request's Host: header to
        # match this virtual host. For the default virtual host (this file) this
        # value is not decisive as it is used as a last resort host regardless.
        # However, you must set it for any further virtual host explicitly.
        #ServerName www.example.com
        Header set Access-Control-Allow-Origin "*"
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html


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

        # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
        # error, crit, alert, emerg.
        # It is also possible to configure the loglevel for particular
        # modules, e.g.
        #LogLevel info ssl:warn

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

        # For most configuration files from conf-available/, which are
        # enabled or disabled at a global level, it is possible to
        # include a line for only one particular virtual host. For example the
        # following line enables the CGI configuration for this host only
        # after it has been globally disabled with "a2disconf".
        #Include conf-available/serve-cgi-bin.conf





</VirtualHost>

Upvotes: 0

Views: 493

Answers (1)

Jerson
Jerson

Reputation: 1745

This works for me, with more than input

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

$1 Represent your request URI

and this is my full code

  <IfModule mod_rewrite.c>

    Options +FollowSymlinks -Indexes
    # Turn mod_rewrite on
    RewriteEngine On
    RewriteBase /

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

    RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC]
    RewriteRule ^ %1 [R=301,L]

  </IfModule>

and you must config your apache2.conf

You must Allow Override All instead of Require all denied

Upvotes: 1

Related Questions