dtch60248 2013-08-08 16:56
浏览 35

url将所有内容重写为index.php,除了少数不适用于saas应用程序

I am using this code in my htaccess file to redirect all requests to index.php and it works.

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)$ index.php [L,QSA]

Now, I am trying to redirect few requests to another file public.php (i..e load everthing via index.php if user is logged in, otherwise load files via public.php - that's the idea)

I have tried several methods which works for others but none of them seems to be working for me., I have no idea why.

the application structure is

[FOLDER]
app
www
static
.htaccess

And this is the .htaccess used in the root folder

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]

By default if the subdomain is www or static files are loaded from www and static folders respectively. If the url is accessed via some other subdomain then the files are loaded from app directory.

The codes i tried are

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^public$ public.php [L,QSA]
RewriteRule ^(.*)$ index.php [L,QSA]

interchanged these two lines as well as tried with removing L, QSA

And many others.., As many i could find on Google + Stackoverflow., But none of them worked, I guess its not working due to how i have coded the .htaccess file in the root directory.

If you guys can find any obvious problems with this htaccess codes please let me know how to fix it, No matter how much i try to learn htaccess i always suck at it, If you know of any good tutorial related to this please post its link as well.., I already Googled and read many articles on it already.

  • 写回答

1条回答 默认 最新

  • dongliping003116 2013-08-08 19:17
    关注

    It took a little to reallize what was wrong, but here it is...

    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]
    

    Thus, the problem with the 3th htaccess, is that "RewriteCond %{REQUEST_FILENAME} !-f" is working on the "public" rewrite, but not on index.php as it should. Hope this solves your problem!

    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c