对于特定的URL' www.example.com/mobile'我希望URL重写为另一个特定的路径:' root_folder/mobile/index.html'
我用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
RewriteRule
模式 与相对于包含.htaccess
文件的目录的 URL 路径进行匹配。因此,上面的RewriteRule
在/mobile/.htaccess
文件中永远不会匹配。同样,条件永远不会成功,因为如果.htaccess
被触发,那么/mobile/
必须已经存在于URL路径中。这也是外部 301 重定向,而不是内部重写。它需要是内部重写(就像重写根目录中的
public/index.php
一样)。但是,想必您还希望忽略静态资源并直接提供服务,因此您需要文件(以及可选的目录)的例外,就像您在根
.htaccess
文件中所做的那样(当重写为 <代码>public/index.php)。请在
/mobile/.htaccess
文件中尝试以下操作:您无需在任何指令中指定
/mobile
,因为RewriteRule
code> 模式 与相对 URL 路径匹配(如上所述),替换 字符串(默认情况下)相对于包含.htaccess 的目录
文件。默认情况下,
/mobile/.htaccess
文件中的 mod_rewrite 指令完全覆盖父.htaccess
文件中的指令。父.htaccess
文件中的指令甚至没有被处理。您应该删除根
.htaccess
文件中的此
块。 (它实际上并没有做任何事情。)The
RewriteRule
pattern matches against the URL-path relative to the directory that contains the.htaccess
file. So, the aboveRewriteRule
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 topublic/index.php
).Try the following instead, in the
/mobile/.htaccess
file:You don't need to specify
/mobile
in any of the directives since theRewriteRule
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.You should remove this
<If>
block in the root.htaccess
file. (It's not actually doing anything.)