对于特定的URL' www.example.com/mobile'我希望URL重写为另一个特定的路径:' root_folder/mobile/index.html'

发布于 2025-01-18 02:28:52 字数 1019 浏览 2 评论 0原文

我用Symfony创建了一个网站,并在Ionic中创建了移动版本。

现在,所有URL都重定向到/public/index.php

我想在.htaccess中添加另一个URL重写,以重定向所有www.example.com/mobile/* to root文件夹中的离子应用/index.html 。

我已经尝试将其添加到root_folder/mobile/.htaccess

RewriteEngine on

RewriteCond %{REQUEST_URI} !^/mobile/.*$
RewriteRule ^mobile/(.*)$ /mobile/index.html [R=301,NC,L]

我的root_folder .htaccess is:

RewriteEngine on

RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteCond %{REQUEST_URI} !^/public/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /public/$1
RewriteCond %{HTTP_HOST} ^(www.)?example.ch$
RewriteCond %{REQUEST_URI} !\.(gif|jpe?g|png|svg|webp|mp4|css|js|txt)$
RewriteRule ^(.*)?$ public/index.php [L]

<If "%{REQUEST_URI} =~ m#/mobile/?#i">
    RewriteCond %{REQUEST_URI} !^/mobile/.*$
    RewriteRule ^mobile/(.*)$ /mobile/index.html [R=301,NC,L]
</If>

I created a website in Symfony and the mobile version in Ionic.

Now all URLs are redirected to /public/index.php.

I want to add another URL rewrite in .htaccess to redirect all www.example.com/mobile/* to ionic app that is in the root folder /mobile/index.html.

I already tried to add this in root_folder/mobile/.htaccess

RewriteEngine on

RewriteCond %{REQUEST_URI} !^/mobile/.*$
RewriteRule ^mobile/(.*)$ /mobile/index.html [R=301,NC,L]

My root_folder .htaccess is:

RewriteEngine on

RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteCond %{REQUEST_URI} !^/public/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /public/$1
RewriteCond %{HTTP_HOST} ^(www.)?example.ch$
RewriteCond %{REQUEST_URI} !\.(gif|jpe?g|png|svg|webp|mp4|css|js|txt)$
RewriteRule ^(.*)?$ public/index.php [L]

<If "%{REQUEST_URI} =~ m#/mobile/?#i">
    RewriteCond %{REQUEST_URI} !^/mobile/.*$
    RewriteRule ^mobile/(.*)$ /mobile/index.html [R=301,NC,L]
</If>

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

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

发布评论

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

评论(1

昔日梦未散 2025-01-25 02:28:52
RewriteCond %{REQUEST_URI} !^/mobile/.*$
RewriteRule ^mobile/(.*)$ /mobile/index.html [R=301,NC,L]

RewriteRule 模式 与相对于包含 .htaccess 文件的目录的 URL 路径进行匹配。因此,上面的 RewriteRule/mobile/.htaccess 文件中永远不会匹配。同样,条件永远不会成功,因为如果.htaccess被触发,那么/mobile/必须已经存在于URL路径中。

这也是外部 301 重定向,而不是内部重写。它需要是内部重写(就像重写根目录中的 public/index.php 一样)。

但是,想必您还希望忽略静态资源并直接提供服务,因此您需要文件(以及可选的目录)的例外,就像您在根 .htaccess 文件中所做的那样(当重写为 <代码>public/index.php)。

请在 /mobile/.htaccess 文件中尝试以下操作:

# /mobile/.htaccess

RewriteEngine On

RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.html [L]

您无需在任何指令中指定 /mobile,因为 RewriteRule code> 模式 与相对 URL 路径匹配(如上所述),替换 字符串(默认情况下)相对于包含 .htaccess 的目录 文件。

默认情况下,/mobile/.htaccess 文件中的 mod_rewrite 指令完全覆盖父 .htaccess 文件中的指令。父 .htaccess 文件中的指令甚至没有被处理。

<如果“%{REQUEST_URI} =~ m#/mobile/?#i”>
    RewriteCond %{REQUEST_URI} !^/mobile/.*$
    RewriteRule ^mobile/(.*)$ /mobile/index.html [R=301,NC,L]

您应该删除根 .htaccess 文件中的此 块。 (它实际上并没有做任何事情。)

RewriteCond %{REQUEST_URI} !^/mobile/.*$
RewriteRule ^mobile/(.*)$ /mobile/index.html [R=301,NC,L]

The RewriteRule pattern matches against the URL-path relative to the directory that contains the .htaccess file. So, the above RewriteRule will never match when inside the /mobile/.htaccess file. Likewise, the condition will never be successful, since if the .htaccess is triggered then /mobile/ must already be present in the URL-path.

This is also an external 301 redirect, not an internal rewrite. It needs to be an internal rewrite (just like the rewrite to public/index.php in the root).

However, presumably you also want static assets to be ignored and served directly, so you need exceptions for files (and optionally directories), just as you are doing in the root .htaccess file (when rewriting to public/index.php).

Try the following instead, in the /mobile/.htaccess file:

# /mobile/.htaccess

RewriteEngine On

RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.html [L]

You don't need to specify /mobile in any of the directives since the RewriteRule pattern matches against the relative URL-path (as mentioned above) and the substitution string is (by default) relative to the directory that contains the .htaccess file.

By default, the mod_rewrite directives in the /mobile/.htaccess file completely override the directives in the parent .htaccess file. The directives in the parent .htaccess file are not even processed.

<If "%{REQUEST_URI} =~ m#/mobile/?#i">
    RewriteCond %{REQUEST_URI} !^/mobile/.*$
    RewriteRule ^mobile/(.*)$ /mobile/index.html [R=301,NC,L]
</If>

You should remove this <If> block in the root .htaccess file. (It's not actually doing anything.)

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