Url-rewrite-a problem that occurs when you use apacherewrite and php to implement a single entry

Source: Internet
Author: User
I want to use Apache rewrite to implement a single entry in php and redirect all requests to the index. php, and then analyze $ _ SERVER [& #039; REQUEST_URI & #039;] to build the routing function. A problem occurs during use. Other addresses are requested, such as localhost... I want to use Apache rewrite to implement a single entry in php and redirect all requests to the index. php, and then analyze $ _ SERVER ['request _ URI '] to build the routing function.

A problem occurs during use. You can obtain the result by requesting other addresses, such as http: // localhost/a/B/c.

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

However, if it is http: // localhost/index, the Error 404 is returned. Why?

This is probably the case with index. php.
 The virtualhost configuration is as follows:

This rewrite rule is found online

DocumentRoot/home/www
  Options FollowSymLinksAllowOverride None # AllowOverride All
  
   
RewriteEngine onRewriteBase/# index. phpRewriteCond % {REQUEST_FILENAME} is not displayed }! -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. php index.html index.htm

Reply content:

I want to use Apache rewrite to implement a single entry in php and redirect all requests to the index. php, and then analyze $ _ SERVER ['request _ URI '] to build the routing function.

A problem occurs during use. You can obtain the result by requesting other addresses, such as http: // localhost/a/B/c.

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

However, if it is http: // localhost/index, the Error 404 is returned. Why?

This is probably the case with index. php.
  The virtualhost configuration is as follows:

This rewrite rule is found online

DocumentRoot/home/www
   Options FollowSymLinksAllowOverride None # AllowOverride All
   
    
RewriteEngine onRewriteBase/# index. phpRewriteCond % {REQUEST_FILENAME} is not displayed }! -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. php index.html index.htm

Your rule has a problem.

RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]It won't work. The correct one should beRewriteRule ^(.*)$ index.php?$1 [QSA,PT,L].?That's it.$1To index. php, and/Tell apache to searchindex.php/The index file under the directory is 404.

You can add the following configuration to httpd. conf, debug mod_rewrite, and view rewrite. the log file can know what the path obtained through rewrite is. After debugging is completed, remove the configuration. Otherwise, performance and security may be affected.

# Output debugging information to rewrite. log
   
    
RewriteLog "/var/log/apache2/rewrite. log" RewriteLogLevel 3
   

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

In addition, there is no need to write the rewrite rule in httpd. conf. It is better to use. htaccess.

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} is not displayed }! -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.