简体   繁体   中英

htaccess mod_rewrite not detects params for project

I started a php project based on http://www.php-mvc.net/ and i searched on Google how to set up my WAMP Server to enable URL rewriting.

I'm running the latest version of WAMP.

1) Enable mod_rewrite module on httpd.conf

2) On httpd-vhosts.conf i added a virtual host

<VirtualHost *:80>
ServerName phpmvc.test
ServerAlias *.phpmvc.test
VirtualDocumentRoot "C:\wamp\www\phpmvc"
ErrorLog "logs\errors.log"
<directory "C:\wamp\www\phpmvc">
    Options Indexes FollowSymLinks
    AllowOverride all
    Order Deny,Allow
    Deny from all
    Allow from all
</directory>

3) On my hosts file i added the host 127.0.0.1 phpmvc.test

4) On .htacces file i have this lines

RewriteEngine on
RewriteRule ^(.*)\/$ index.php?url=$1 [QSA,L]

I restarted Apache after setting up the files and when i went to phpmvc.test it worked fine but when i tried phpmvc.test/test it shows an error that says that the requested URL is not found on this server.

EDIT: I tried with RewriteRule ^(.*)$ index.php?url=$1 [QSA,L] but it shows the same error EDIT2: I tried with EDIT: I tried with Rahil Wazir suggestion but it stills not working.

I have this controller file IndexController.class.php and it works if i access with this URL: phpmvc.test?url=index but if i try phpmvc.test/index it says that index is not found on the server.

<?php

class IndexController {

    public static function index() {

        echo "Index controller";

    }
}

Printing $_GET and $_SERVER on root i get an empty Array() and the server array. If i go to phpmvc.test/index?url=index i get the value on $_GET["url"] and on $_SERVER [QUERY_STRING] => url=index .

This is because Apache thinks /test is a file or directory which you are trying to accessing which probably don't exist.

You need to apply those two famous condition which is used by most Front controller pattern frameworks:

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

Which tells Apache Rewrite Module unless the file or directory from the requested uri is exist then apply the rewrite rule, otherwise render the contents of the file or directory.

Debug by checking what you are getting in $_GET and $_SERVER variables.

print_r($_GET);
print_r($_SERVER);

Compare the differences in the values for ['url'] index. Also, it may depend on your sub-folder depth as seen on the URL before /index.

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