如何重定向到以.htaccess的范围为子目录索引文件

发布于 2025-02-01 16:03:44 字数 959 浏览 3 评论 0 原文

我有两个网页。 我想在一个域中部署这两个页面。 当我调用root URL时,我想在root Directory中加载 index.html ,对于其他URL,我想在 index1.html >/app 目录。

这是目录结构。

www.example.com/index.html
www.example.com/app/index1.html

例如: 请求 www.example.com 加载 index.html

www.example.com/login 加载/app/index1.html

www.example.com/signup 加载/app/index1.html

这是我尝试的。

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /
  RewriteRule ^index\.html$ - [L]
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-l
  RewriteRule ^(.*)$ /app/index1.html [R=301,L]
</IfModule>

当我请求 www.example.com/signup www.example.com/app/index1.html 时。

但是我想加载 app/index1.html 而无需重定向。请帮我。

I have two web pages.
I want to deploy these two pages on one domain.
When I call the root URL, I want to load the index.html in root directory, and for other URLs, I want to load the index1.html in the /app directory.

This is the directory structure.

www.example.com/index.html
www.example.com/app/index1.html

For example:
when request www.example.com
loading index.html

For www.example.com/login
loading /app/index1.html

For www.example.com/signup
loading /app/index1.html

This is what I have tried.

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /
  RewriteRule ^index\.html$ - [L]
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-l
  RewriteRule ^(.*)$ /app/index1.html [R=301,L]
</IfModule>

This make redirection when I request www.example.com/signup to www.example.com/app/index1.html.

But I want to load app/index1.html without redirection. Please help me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

霓裳挽歌倾城醉 2025-02-08 16:03:44

使用您显示的样本,请尝试以下内容。htaccess规则。在测试URL之前,请确保以下内容:

  • 确保您的 .htaccess 文件, index.html app 文件夹居住在相同 root 文件夹。
  • 确保您有 index1.html /app 文件夹中的文件。
  • 在测试URL之前,请确保清除浏览器缓存。
RewriteEngine ON
##Rule from OP's attempt to block direct access of index.html file.
RewriteRule ^index\.html$ - [NC,L]

##Rule to handle only url www.example.com here.
RewriteRule ^/?$  /index.html [QSA,NC,L]

##Rules to handle rest of the cases here..
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^  /app/index1.html [QSA,NC,L]

With your shown samples, please try following .htaccess rules. Please make sure of following things before you test your URLs:

  • Make sure your .htaccess file, index.html and your app folder are residing in same root folder.
  • Make sure you have index1.html file present inside /app folder.
  • Make sure to clear your browser cache before testing your URLs.

RewriteEngine ON
##Rule from OP's attempt to block direct access of index.html file.
RewriteRule ^index\.html$ - [NC,L]

##Rule to handle only url www.example.com here.
RewriteRule ^/?$  /index.html [QSA,NC,L]

##Rules to handle rest of the cases here..
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^  /app/index1.html [QSA,NC,L]
迟到的我 2025-02-08 16:03:44
 &lt; ifmodule mod_rewrite.c&gt;
  重新创新
  RewriteBase /
  重写 ^index \ .html $  -  [l]
  重新WritriteCond%{request_filename}!-f
  rewriteCond%{request_filename}!-d
  REWRITECOND%{request_filename}!-L
  重写 ^(。*)$ /app/index1.html [r = 301,l]
&lt;/ifmodule&gt;
 

基本上,您只需要删除最后一个 Rewriterule 指令上的 r redirect )标志。但这可以优化:

DirectoryIndex index.html

RewriteEngine On

RewriteRule ^app/index1\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule . app/index1.html [L]

mod_dir(即 directoryIndex )从根中提供 /index.html 。这可能已经在服务器配置中配置了,因此在这里不需要 DirectoryIndex 指令。

  • 第一个重写指令是一种优化,以防止不必要的文件系统检查。这应该匹配要重写的文件。 IE。 /app/index1.html (不是 /index.html )。

  • 最后一个重写与单个字符(即 - 点)匹配,因此排除了根目录的请求,因此每次请求root均可防止不必要的文件系统检查。另一方面,正则^(。代码> REWRITECOND 指令)。

  • 除非您使用符号链接,否则您可以删除第三条件

  • 根据您的URL格式,您可以使正则限制性更加限制,并删除了检查请求未映射到文件的第一个条件(文件系统检查相对昂贵)。例如。您的URL包含点吗?您给出的两个例子没有。点自然会划界文件扩展名,因此,如果您的URL不包含点,则它们自然不会映射到任何现有文件。

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /
  RewriteRule ^index\.html$ - [L]
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-l
  RewriteRule ^(.*)$ /app/index1.html [R=301,L]
</IfModule>

You basically just needed to remove the R (redirect) flag on the last RewriteRule directive. But this can be optimised:

DirectoryIndex index.html

RewriteEngine On

RewriteRule ^app/index1\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule . app/index1.html [L]

mod_dir (ie. DirectoryIndex) serves /index.html from the root. This may already be configured in the server config, so the DirectoryIndex directive may not be required here.

  • The first RewriteRule directive is an optimisation to prevent unnecessary filesystem checks. This should match the file being rewritten to. ie. /app/index1.html (not /index.html).

  • The last RewriteRule matches against a single character (ie. . - dot) so excludes requests for the root directory so prevents unnecessary filesystem checks everytime the root is requested. The regex ^(.*)$ on the other hand matches everything, including the root (which fails the directory check - 2nd condition / RewriteCond directive).

  • Unless you are using symlinks then you can remove the 3rd condition.

  • Depending on the format of your URLs then you could make the regex more restrictive and perhaps remove the first condition that checks that the request does not map to a file (filesystem checks are relatively expensive). eg. Do your URLs contain dots? The two examples you gave do not. Dots naturally delimit file extensions, so if your URLs do not contain dots then they will naturally not map to any existing files.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文