【问题标题】:RewriteRule in sub folder子文件夹中的重写规则
【发布时间】:2023-03-13 05:17:01
【问题描述】:

我有问题。

有一个自定义的 mvc 结构,当它是根文件夹时,一切都可以通过 .htaccess 中的 RewriteRule 工作,但如果我将它设置为子文件夹,它就会停止工作。

.htaccess 是

重写引擎开启
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?route=$1 [L,QSA]
添加默认字符集 UTF-8
然后我更改了基础并添加了:
RewriteBase /mysubfolder/mySubSubfolder/

丛文件:
服务器管理员 root@domain.com
DocumentRoot /var/www/rootfolder/

选项索引 FollowSymLinks 多视图
允许覆盖全部
要求全部授予
订单允许,拒绝
全部允许
目录>

选项索引 FollowSymLinks 多视图
允许覆盖全部
要求全部授予
订单允许,拒绝
全部允许
目录>

我尝试了一些有关重写规则的魔术,但似乎我缺乏知识。 如果你能帮忙就好了。

谢谢,对不起,我有点菜鸟:)

【问题讨论】:

    标签: .htaccess mod-rewrite


    【解决方案1】:

    tldr;

    假设 .htaccess 文件没有移动到 MVC 应用程序的子目录...

    更新全局重定向行:

    RewriteRule ^(.*)$ index.php?route=$1 [L,QSA]
    

    到:

    RewriteRule ^(.*)$ subdir/index.php?route=$1 [L,QSA]
    

    对于大多数(如果不是 PHP 中的所有现代 MVC 框架),“所有”非文件路径 (URL) 都被设计为重定向到“index.php”文件。你可能会注意到这一行:

    RewriteRule ^(.*)$ index.php?route=$1 [L,QSA]
    

    它的设计本质上是采用常规 URL,例如http://example.com/some/path 并将其重定向到 http://example.com/index.php?route=some/path

    它前面的两行说明: "如果 URL 不是文件请求"

    RewriteCond %{REQUEST_FILENAME} !-f
    

    “如果 URL”不是对目录的请求”

    RewriteCond %{REQUEST_FILENAME} !-d
    

    然后重定向任何匹配正则表达式的东西:^(.*)$ 并将其重定向到目标路径

    RewriteRule ^(.*)$ index.php?route=$1 [L,QSA]
    

    我假设您移动了项目(但不是 .htaccess 文件)。

    假设如上,如果你把你的项目移动到子目录中,你需要更新目标路径,使index.php路径正确。

    例如

    RewriteRule ^(.*)$ subdir/index.php?route=$1 [L,QSA]
    

    --

    给出正则表达式的速成分解:

    ^ indicates the string must start with the string provided, e.g. ^apple would mean the string would only match if it starts with the word apple
    
    $ indicates the string must end with the string provided, e.g. banana$ would mean the string would only match if it ended with the word banana
    
    . indicates any character
    
    * following the "." means any number of characters (1 to infinity, theoretically)
    

    简而言之,^(.*)$ 意味着匹配几乎所有内容!

    【讨论】:

    • 非常感谢伙计,虽然我已经找到了答案,而且它比我想象的更愚蠢,只是关于链接,我非常感谢你的回答,因为它帮助我更深入地理解“重写规则”,谢谢
    猜你喜欢
    • 2012-11-20
    • 2012-11-22
    • 1970-01-01
    • 2014-04-08
    • 1970-01-01
    • 1970-01-01
    • 2016-08-16
    • 2012-02-24
    • 1970-01-01
    相关资源
    最近更新 更多