我正在使用PHP进行一个小的自定义MVC项目,并且我遇到了一些与htaccess有关的问题。最初,我的htaccess将所有流量路由到我的根目录中的index.php,并将剩余路径作为参数传递。这与这段代码非常吻合:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?path=$1 [NC,L,QSA]
我现在遇到的问题是我需要将index.php文件移动到/ public目录中。我在互联网上搜索了答案,并找到了有点的代码片段,只要它只是点击/它似乎就到达那里,但是一旦url成为/ register /或任何东西否则它只是404。
# Get rid of /public/ in the URL, and route all requests through
# the Index.php file
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /public/index.php?path=$1 [NC,L,QSA]
RewriteCond %{REQUEST_URI} !/public/ [NC]
RewriteRule ^(.*?)/?$ public/$1 [L]
我知道最后一行没有任何意义,当你对index.php进行重写时,路径看起来对我来说是合适的(至少它会像我想要的那样传递参数!)但是如果没有这两行它就不会#39似乎工作,我已经试了好几个小时。希望有人可以帮忙!干杯!
答案 0 :(得分:2)
仅在.htaccess中保留此内容:
DirectoryIndex index.php
RewriteEngine On
RewriteRule ^/?$ public/ [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ public/?path=$1 [L,QSA]