A Problem Occurred when Using apache rewrite (visual novel) and php to Realize Single Entry

  ios, question

I want to use Apache’s rewrite (visual novel) to implement a single portal in php, redirect all requests to index.php, and then analyze $_SERVER[‘REQUEST_URI’] to build a routing function.

There was a problem when using it, requesting other addresses, such ashttp://localhost/a/b/cResults can be obtained

array (size=4)
  0 => string '' (length=0)
  1 => string 'a' (length=1)
  2 => string 'b' (length=1)
  3 => string 'c' (length=1)

But if it ishttp://localhost/indexWill prompt 404 error, this is why

This is probably the case in index.php

<?php
........

function analysisUri() {
    var_dump(explode('/', $_SERVER['REQUEST_URI']));
}

echo $_SERVER['REQUEST_URI'];

analysisUri();

........

The virtualhost configuration is like this

This rewrite (visual novel) rule was found online

DocumentRoot /home/www
<Directory />
	Options FollowSymLinks
	AllowOverride None
	#AllowOverride All 

	<IfModule mod_rewrite.c>
		RewriteEngine on

		RewriteBase /
		#不显示index.php

		RewriteCond %{REQUEST_FILENAME} !-d
		RewriteCond %{REQUEST_FILENAME} !-f
		RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]
		#RewriteRule ^(.*)$ index.php?$1 [QSA,L]
	</IfModule>
</Directory>
<Directory /home/www/>
	Options Indexes FollowSymLinks MultiViews
	AllowOverride None
	#AllowOverride All 
	Order allow,deny
	allow from all

	DirectoryIndex index.php index.html index.htm
</Directory>

There is something wrong with your rules.

RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]It won’t work, the correct thing should beRewriteRule ^(.*)$ index.php ? $1 [QSA,PT,L].?Only then, the latter$1To index.php, and/Tell apache to find outindex.php/Directory index file, nature is 404.

You can add the following configuration to httpd.conf and debug mod_rewrite (visual novel). By looking at the rewrite (visual novel). log file, you can know exactly what the path is through Rewrite (visual novel). Remove this configuration after debugging is completed, otherwise there will be some performance and security impacts.

# 输出调试信息到 rewrite.log
<IfModule mod_rewrite.c>
    RewriteLog "/var/log/apache2/rewrite.log"
    RewriteLogLevel 3
</IfModule>

http://httpd.apache.org/docs/2.2/mod/ …

In addition, there is no need for the rewrite (visual novel) Rules to write that. htaccess is better in httpd.conf.

httpd.conf

DocumentRoot /home/www

<Directory />
    Options FollowSymLinks
    AllowOverride None
    #AllowOverride All 
</Directory>

<Directory /home/www/>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride None
    #AllowOverride All 
    Order allow,deny
    allow from all

    DirectoryIndex index.php index.html index.htm
</Directory>

/home/www/.htaccess

<IfModule mod_rewrite.c>
        RewriteEngine on

        #不显示index.php
        RewriteBase /

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