Url-rewrite-an issue that occurs when using Apache rewrite and PHP to implement a single entry

Source: Internet
Author: User
I want to use Apache rewrite to implement a single portal in PHP, redirect all requests to index.php, and then analyze $_server[' Request_uri '] to build the routing function.

At the time of use there is a problem, request other addresses, such as http://localhost/a/b/c can get results

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's http://localhost/index, you'll be prompted with a 404 error, which is why.

Index.php, that's probably it.

 

VirtualHost configuration is like this

This rewrite rule was found on the Internet.

Documentroot/home/www
 
   Options followsymlinksallowoverride none#allowoverride 
 
   
    
  All Rewriteengine onrewritebase/#不显示index. Phprewritecond%{request_filename}!-drewritecond%{request_filename}!- Frewriterule ^ (. *) $ index.php/$1 [qsa,pt,l] #RewriteRule ^ (. *) $ index.php?$1 [qsa,l]
 
   
 
   Options Indexes followsymlinks multiviewsallowoverride none#allowoverride all Order allow,denyallow from Alldirectoryindex index.php index.html index.htm

Reply content:

I want to use Apache rewrite to implement a single portal in PHP, redirect all requests to index.php, and then analyze $_server[' Request_uri '] to build the routing function.

At the time of use there is a problem, request other addresses, such as http://localhost/a/b/c can get results

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's http://localhost/index, you'll be prompted with a 404 error, which is why.

Index.php, that's probably it.

  

VirtualHost configuration is like this

This rewrite rule was found on the Internet.

Documentroot/home/www
 
    Options followsymlinksallowoverride none#allowoverride 
 
    
     
  All Rewriteengine onrewritebase/#不显示index. Phprewritecond%{request_filename}!-drewritecond%{request_filename}!- Frewriterule ^ (. *) $ index.php/$1 [qsa,pt,l] #RewriteRule ^ (. *) $ index.php?$1 [qsa,l]
 
    
 
    Options Indexes followsymlinks multiviewsallowoverride none#allowoverride all Order allow,denyallow from Alldirectoryindex index.php index.html index.htm

There's something wrong with your rules.

RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]The right thing to do is RewriteRule ^(.*)$ index.php?$1 [QSA,PT,L] . ?It's going to go back to the $1 index.php, and / tell Apache to find index.php/ the index file under the directory, which is naturally 404.

You can add the following configuration to httpd.conf, Debug mod_rewrite, by looking at the Rewrite.log file can know what the path through rewrite, and remove this configuration after debugging, otherwise there will be some performance security impact.

# output debug information to Rewrite.log
 
    
     
      rewritelog "/var/log/apache2/rewrite.log"    rewriteloglevel 3
 
    

Http://httpd.apache.org/docs/2.2/mod/...

In addition, rewrite rules do not necessarily write httpd.conf inside, use. htaccess better.

httpd.conf

Documentroot/home/www
 
        Options followsymlinks    allowoverride None    #AllowOverride 
 All 
        Options Indexes followsymlinks multiviews    allowoverride None    #AllowOverride all     Order Allow,deny allow from all    directoryindex index.php index.html index.htm

/home/www/.htaccess

 
    
     
          Rewriteengine on        #不显示index. PHP        rewritebase/        rewritecond%{request_filename}!-d        Rewritecond%{ Request_filename}!-f                rewriterule ^ (. *) $ index.php?$1 [qsa,pt,l]
 
    

Rewritecond%{script_filename}!-frewritecond%{script_filename}!-drewriterule ^ (. *) $ index.php/$1
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.