我在htaccess文件中使用这段代码将所有请求重定向到index.php,它很有效。
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [L,QSA]
现在,我试图将一些请求重定向到另一个文件public.php
(即,如果用户登录,则通过index.php加载everthing,否则通过public.php加载文件-这就是想法)
我试过几种对别人有效的方法,但似乎没有一种对我有效。我不知道为什么。
应用程序结构是
[FOLDER]
app
www
static
.htaccess
这是根文件夹中使用的.htaccess
RewriteCond %{HTTP_HOST} ^(www.)?website.com
RewriteRule ^(.*)$ www/index.php?$1 [L,QSA]
RewriteCond %{HTTP_HOST} ^(static.)?website.com
RewriteRule ^(.*)$ static/index.php?$1 [L,QSA]
RewriteCond %{HTTP_HOST} ^([^.]+)'.website'.com$ [NC]
RewriteRule ^(.*)$ app/index.php?$1 [L,QSA]
默认情况下,如果子域为www
或static
,则分别从www
和static
文件夹加载文件。如果url是通过其他子域访问的,那么文件将从app
目录加载。
我试过的代码是
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^public$ public.php [L,QSA]
RewriteRule ^(.*)$ index.php [L,QSA]
互换了这两条线路,并尝试删除L、QSA
还有许多其他。。,正如我在Google+Stackoverflow上能找到的一样多。,但它们都不起作用,I guess its not working due to how i have coded the .htaccess file in the root directory
。
如果你们能发现这个htaccess代码有任何明显的问题,请让我知道如何修复它,无论我多么努力地学习htaccess,我总是很糟糕,如果你知道任何与此相关的好教程,请发布它的链接。。,我已经在谷歌上搜索并阅读了许多关于它的文章
花了一点时间才意识到问题所在,但现在是…
RewriteEngine On
# first line: If the filename contains public.php, dont rewrite again.
# second line: If the requested URI contains /public, rewrite to public.php
RewriteCond %{REQUEST_FILENAME} !/public.php [NC]
RewriteCond %{REQUEST_URI} ^/public
RewriteRule ^(.*)$ public.php?$1 [L]
# Now here layed the problem, you do NOT want to rewrite the requested URI IF it is pointing to an actual file on the server.
# Thus, a condition for rewriting to index.php, is that the file does not exist.
# The second condition is that the directory does not exist. Not sure you need that though.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$ [L]
因此,第三次htaccess的问题是"RewriteCond%{REQUEST_FILENAME}!-f"正在进行"公共"重写,但没有像它应该的那样进行index.php。希望这能解决你的问题!