如何编写 htaccess 重写规则,您可以在其中将网站加载到文件夹中,就像从根目录加载一样
How to write a htaccess rewrite rule where you can load a website inside a folder as it would be loaded from the root
我有以下文件夹结构
ROOT (loaded on domain.com)
+campaigns
|+campaign1
|-assets
|-index.php
|-campaign2
.htaccess
index.php
stuff.php
目前要访问文件夹 campaign1
中的网站,我必须在 URL 地址栏中输入:domain.com/campaigns/campaign1
我应该在 .htaccess
文件中放什么,这样当你放
domain.com/campaign1
浏览器加载并显示 domain.com/campaigns/campaign1
中的所有内容,但当然不会明显更改地址栏中的 URL。
非常感谢您的帮助。
您可以使用 mod_rewrite 在根 .htaccess
文件中执行类似以下操作。这是一个通用版本,如果“campaign”子目录未知(或太多):
RewriteEngine On
# 1. Abort early if request already maps to a file or directory
RewriteRule ^campaigns/ - [L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
# 2. Check if the first path-segment maps to a directory in "/campaigns` directory
# If so then internally rewrite the request to that subdirectory
RewriteCond %{DOCUMENT_ROOT}/campaigns/ -d
RewriteRule ^([^/]+)/ campaigns%{REQUEST_URI} [L]
其中 </code> 是对 <code>RewriteRule
模式 .
中捕获的子组的反向引用
i would have to enter in the URL address bar: example.com/campaigns/campaign1
因为 campaign1
是一个目录,您应该请求 campaign1/
(带有尾部斜线)否则 mod_dir 将发出外部重定向以附加尾部斜线。
when you put example.com/campaign1
the browser loads and shows everything from ...
同样,您应该请求 example.com/compaign1/
- 带有尾部斜杠 - 并且上述规则假设您是。如果省略尾部斜杠,它不会执行任何操作,您将获得 404。(如果您期望第三方请求省略尾部斜杠,那么您将需要手动发出 301 重定向以附加尾部斜杠 在上述规则之前。)
更新:
What if i know the name of the campaign folder that goes into the campaigns folder? basically if i want to manually add the rule but campaign specific?
是的,你可以做到。事实上,如果您希望以这种方式重写的活动数量有限,那么这甚至可能更可取,因为它避免了文件系统检查。
您还可以删除第二条规则(上面第 1 节的第二部分),该规则在请求文件或目录时阻止进一步处理。 IE。删除以下内容:
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
因此,您可以执行以下操作而不是(或之前)第 2 节...
对于单个广告系列:
# 2. Rewrite campaign to "/campaigns" subdirectory
RewriteRule ^compaign1/ campaigns%{REQUEST_URI} [L]
对于多个广告系列:
# 2. Rewrite campaigns to "/campaigns" subdirectory
RewriteCond =campaign1 [OR]
RewriteCond =campaign2 [OR]
RewriteCond =campaign3
RewriteRule ^([^/]+)/ campaigns%{REQUEST_URI} [L]
请注意,它具体是 =campaign1
,=
是 CondPattern(第二个参数)本身的 prefix-operator。 =
运算符使其成为精确的字符串匹配,而不是正则表达式。
或者,使用正则表达式和交替:
# 2. Rewrite campaigns to "/campaigns" subdirectory
RewriteCond ^(campaign1|campaign2|campaign3)$
RewriteRule ^([^/]+)/ campaigns%{REQUEST_URI} [L]
(虽然这可以组合成一个指令。)
更新#2:
在内部重写之前添加此内容以在 URL:
的末尾添加尾部斜杠
RewriteCond %{REQUEST_URI} !\.
RewriteRule !/$ %{REQUEST_URI}/ [R=301,L]
请注意,所有内部链接 必须 已经链接到 URL 并带有 尾部斜杠。此重定向仅是为了搜索引擎和第三方链接的利益,这些链接可能引用 non-canonical URL 而没有尾部斜杠。
我有以下文件夹结构
ROOT (loaded on domain.com)
+campaigns
|+campaign1
|-assets
|-index.php
|-campaign2
.htaccess
index.php
stuff.php
目前要访问文件夹 campaign1
中的网站,我必须在 URL 地址栏中输入:domain.com/campaigns/campaign1
我应该在 .htaccess
文件中放什么,这样当你放
domain.com/campaign1
浏览器加载并显示 domain.com/campaigns/campaign1
中的所有内容,但当然不会明显更改地址栏中的 URL。
非常感谢您的帮助。
您可以使用 mod_rewrite 在根 .htaccess
文件中执行类似以下操作。这是一个通用版本,如果“campaign”子目录未知(或太多):
RewriteEngine On
# 1. Abort early if request already maps to a file or directory
RewriteRule ^campaigns/ - [L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
# 2. Check if the first path-segment maps to a directory in "/campaigns` directory
# If so then internally rewrite the request to that subdirectory
RewriteCond %{DOCUMENT_ROOT}/campaigns/ -d
RewriteRule ^([^/]+)/ campaigns%{REQUEST_URI} [L]
其中 </code> 是对 <code>RewriteRule
模式 .
i would have to enter in the URL address bar:
example.com/campaigns/campaign1
因为 campaign1
是一个目录,您应该请求 campaign1/
(带有尾部斜线)否则 mod_dir 将发出外部重定向以附加尾部斜线。
when you put
example.com/campaign1
the browser loads and shows everything from ...
同样,您应该请求 example.com/compaign1/
- 带有尾部斜杠 - 并且上述规则假设您是。如果省略尾部斜杠,它不会执行任何操作,您将获得 404。(如果您期望第三方请求省略尾部斜杠,那么您将需要手动发出 301 重定向以附加尾部斜杠 在上述规则之前。)
更新:
What if i know the name of the campaign folder that goes into the campaigns folder? basically if i want to manually add the rule but campaign specific?
是的,你可以做到。事实上,如果您希望以这种方式重写的活动数量有限,那么这甚至可能更可取,因为它避免了文件系统检查。
您还可以删除第二条规则(上面第 1 节的第二部分),该规则在请求文件或目录时阻止进一步处理。 IE。删除以下内容:
RewriteCond %{REQUEST_FILENAME} -f RewriteCond %{REQUEST_FILENAME} -d RewriteRule ^ - [L]
因此,您可以执行以下操作而不是(或之前)第 2 节...
对于单个广告系列:
# 2. Rewrite campaign to "/campaigns" subdirectory
RewriteRule ^compaign1/ campaigns%{REQUEST_URI} [L]
对于多个广告系列:
# 2. Rewrite campaigns to "/campaigns" subdirectory
RewriteCond =campaign1 [OR]
RewriteCond =campaign2 [OR]
RewriteCond =campaign3
RewriteRule ^([^/]+)/ campaigns%{REQUEST_URI} [L]
请注意,它具体是 =campaign1
,=
是 CondPattern(第二个参数)本身的 prefix-operator。 =
运算符使其成为精确的字符串匹配,而不是正则表达式。
或者,使用正则表达式和交替:
# 2. Rewrite campaigns to "/campaigns" subdirectory
RewriteCond ^(campaign1|campaign2|campaign3)$
RewriteRule ^([^/]+)/ campaigns%{REQUEST_URI} [L]
(虽然这可以组合成一个指令。)
更新#2:
在内部重写之前添加此内容以在 URL:
的末尾添加尾部斜杠RewriteCond %{REQUEST_URI} !\.
RewriteRule !/$ %{REQUEST_URI}/ [R=301,L]
请注意,所有内部链接 必须 已经链接到 URL 并带有 尾部斜杠。此重定向仅是为了搜索引擎和第三方链接的利益,这些链接可能引用 non-canonical URL 而没有尾部斜杠。