我在我的htaccess文件中使用此代码将所有请求重定向到index.php并且它可以正常工作。
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [L,QSA]
现在,我正在尝试将少量请求重定向到另一个文件public.php
(如果用户登录,则通过index.php加载,否则通过public.php加载文件 - 这就是想法)< / p>
我尝试了几种适用于其他人的方法,但似乎没有一种方法适合我。我不知道为什么。
应用程序结构是
[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
。
答案 0 :(得分:1)
花了一点时间来重新分析错误,但这里是......
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]
因此,第3个htaccess的问题在于,“RewriteCond%{REQUEST_FILENAME}!-f”正在进行“公共”重写,而不是像index.php那样。 希望这能解决你的问题!