子文件夹中的RewriteRule

问题描述 投票:0回答:1

我有一个问题。

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

.htaccess是

RewriteEngine On RewriteCond %{REQUEST_FILENAME}。!-f RewriteCond %{REQUEST_FILENAME}。!-d RewriteRule ^(.*)$ index.php?route=$1 [L,QSA] AddDefaultCharset UTF-8 然后我改变了基础,并添加了。RewriteBase mysubfoldermySubSubfolder

聪聪文件。 ServerAdmin [email protected] DocumentRoot varwwwrootfolder < Directory varwwwrootfolder > Options Indexes FollowSymLinks MultiViews AllowOverride All Require all granted Order allow,deny allow from all < Directory > < Directory varwwwrootfolderssub > Options Indexes FollowSymLinks MultiViews AllowOverride All Require all granted Order allow,deny allow from all < Directory >

我尝试了一些神奇的技巧与重写规则,但它似乎我有一个缺乏知识.将是巨大的,如果你能帮助。

谢谢你,对不起,我是一个有点noob:)

.htaccess mod-rewrite
1个回答
0
投票

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.comsomepath 并将其重定向到 http:/example.comindex.php?route=somepath

前面的2行写着:"如果URL不是一个文件的请求"

RewriteCond %{REQUEST_FILENAME} !-f

"如果URL "不是一个目录的请求"

RewriteCond %{REQUEST_FILENAME} !-d

然后重定向任何与regex匹配的内容。^(.*)$并重定向到目标路径

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)

简而言之,^(.*)$意味着几乎所有的东西都匹配!

© www.soinside.com 2019 - 2024. All rights reserved.