简体   繁体   English

htaccess mod_rewrite未检测到项目参数

[英]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. 我基于http://www.php-mvc.net/启动了一个php项目,并在Google上搜索了如何设置WAMP Server以启用URL重写。

I'm running the latest version of WAMP. 我正在运行最新版本的WAMP。

1) Enable mod_rewrite module on httpd.conf 1)在httpd.conf上启用mod_rewrite模块

2) On httpd-vhosts.conf i added a virtual host 2)在httpd-vhosts.conf我添加了一个虚拟主机

<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 3)在我的主机文件中,我添加了主机127.0.0.1 phpmvc.test

4) On .htacces file i have this lines 4)在.htacces文件上我有这行

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. 设置完文件后,我重新启动了Apache,当我去phpmvc.test它工作正常,但是当我尝试phpmvc.test/test它显示了一条错误,指出该服务器上找不到所请求的URL。

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. 编辑:我尝试了RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]但它显示了相同的错误EDIT2:我尝试了编辑:我尝试了Rahil Wazir的建议,但仍然无法正常工作。

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. 我有此控制器文件IndexController.class.php ,如果我使用以下URL访问它,则该文件有效: phpmvc.test?url=index但是如果我尝试phpmvc.test/index它说在服务器上找不到该index

<?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. 在根目录上打印$_GET$_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 . 如果我去phpmvc.test/index?url=index我得到$_GET["url"]$_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. 这是因为Apache认为/test是您要访问的文件或目录,可能不存在。

You need to apply those two famous condition which is used by most Front controller pattern frameworks: 您需要应用大多数Front Controller模式框架使用的两个著名条件:

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. 它告诉Apache重写模块,除非存在来自所请求uri的文件或目录,然后应用重写规则,否则呈现文件或目录的内容。

Debug by checking what you are getting in $_GET and $_SERVER variables. 通过检查$ _GET和$ _SERVER变量中得到的内容进行调试。

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

Compare the differences in the values for ['url'] index. 比较['url']索引值的差异。 Also, it may depend on your sub-folder depth as seen on the URL before /index. 另外,它可能取决于您在/ index前面的URL上看到的子文件夹深度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM