Apache 使用内部重定向重写规则无法正常工作
我相信我正在寻找的解决方案非常简单,但我已经花了几乎一天的时间寻找它,但没有成功,所以我投降了 我想将所有请求从Apache 使用内部重定向重写规则无法正常工作,apache,.htaccess,redirect,mod-rewrite,Apache,.htaccess,Redirect,Mod Rewrite,我相信我正在寻找的解决方案非常简单,但我已经花了几乎一天的时间寻找它,但没有成功,所以我投降了 我想将所有请求从portail.domain.com重定向到portail index.php文件,并将所有其他请求(从我的其他子域)重定向到index.php文件 这是我的.htaccess文件: # ROUTING portail SUBDOMAIN TO portail-index.php RewriteCond %{HTTP_HOST} ^portail.* RewriteCond %{REQ
portail.domain.com
重定向到portail index.php
文件,并将所有其他请求(从我的其他子域)重定向到index.php
文件
这是我的.htaccess
文件:
# ROUTING portail SUBDOMAIN TO portail-index.php
RewriteCond %{HTTP_HOST} ^portail.*
RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_URI} ^portail-index.php$
RewriteCond %{REQUEST_FILENAME} !-d [OR]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule .* /portail-index.php [NC,L,QSA]
# REROUTING EVERYTHING ELSE TO index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d [OR]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule .* /index.php [NC,L,QSA]
strip per-dir prefix: /home/ubuntu/domain/app/preprod/ ->
applying pattern '^' to uri ''
RewriteCond: input='/home/ubuntu/domain/app/preprod/' pattern='-f' => not-matched
RewriteCond: input='/home/ubuntu/domain/app/preprod/' pattern='-d' => matched
pass through /home/ubuntu/domain/app/preprod/
strip per-dir prefix: /home/ubuntu/domain/app/preprod/index.php -> index.php
applying pattern '^' to uri 'index.php'
RewriteCond: input='/home/ubuntu/domain/app/preprod/index.php' pattern='-f' => matched
pass through /home/ubuntu/domain/app/preprod/index.php
下面是我尝试访问portail.domain.com
时的日志结果:
applying pattern '.*' to uri ''
RewriteCond: input='portail.domain.com' pattern='^portail.*' => matched
RewriteCond: input='/home/ubuntu/app/preprod/' pattern='!-f' => matched
RewriteCond: input='/home/ubuntu/app/preprod/' pattern='!-d' => not-matched
RewriteCond: input='/' pattern='^/$' => matched
rewrite '' -> '/portail-index.php'
trying to replace prefix /home/ubuntu/domain/app/preprod/ with /
internal redirect with /portail-index.php [INTERNAL REDIRECT]
strip per-dir prefix: /home/ubuntu/domain/app/preprod/index.php -> index.php
applying pattern '.*' to uri 'index.php'
RewriteCond: input='portail.domain.com' pattern='^portail.*' => matched
RewriteCond: input='/home/ubuntu/domain/app/preprod/index.php' pattern='!-f' => not-matched
RewriteCond: input='/index.php' pattern='^portail-index.php$' => not-matched
strip per-dir prefix: /home/ubuntu/domain/app/preprod/index.php -> index.php
applying pattern '.*' to uri 'index.php'
RewriteCond: input='/home/ubuntu/domain/app/preprod/index.php' pattern='!-f' => not-matched
pass through /home/ubuntu/domain/app/preprod/index.php
现在我完全明白,导致内部重定向
会重演一系列重写规则我不明白的是,为什么第二次通过与portail.domain.com
规则不匹配。
我尝试用END
标志替换L
标志,但没有效果。结果完全一样。我还尝试了几次改写规则,但没有一次成功。我花了几个小时阅读ApacheRewrite_mod文档,所以我相信我对该模块非常了解,但在这种情况下无法使其正常工作
任何关于它为什么不起作用的建议,或者更好的建议:如何使它起作用,都是非常受欢迎的
---编辑---
strip per-dir prefix: /home/ubuntu/domain/app/preprod/ ->
applying pattern '^' to uri ''
RewriteCond: input='/home/ubuntu/domain/app/preprod/' pattern='-f' => not-matched
RewriteCond: input='/home/ubuntu/domain/app/preprod/' pattern='-d' => matched
pass through /home/ubuntu/domain/app/preprod/
strip per-dir prefix: /home/ubuntu/domain/app/preprod/index.php -> index.php
applying pattern '^' to uri 'index.php'
RewriteCond: input='/home/ubuntu/domain/app/preprod/index.php' pattern='-f' => matched
pass through /home/ubuntu/domain/app/preprod/index.php
非常感谢您的帮助@Anubhava。以下是我在实现您的.htaccess
文件版本后获得的日志:
# ROUTING portail SUBDOMAIN TO portail-index.php
RewriteCond %{HTTP_HOST} ^portail.*
RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_URI} ^portail-index.php$
RewriteCond %{REQUEST_FILENAME} !-d [OR]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule .* /portail-index.php [NC,L,QSA]
# REROUTING EVERYTHING ELSE TO index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d [OR]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule .* /index.php [NC,L,QSA]
strip per-dir prefix: /home/ubuntu/domain/app/preprod/ ->
applying pattern '^' to uri ''
RewriteCond: input='/home/ubuntu/domain/app/preprod/' pattern='-f' => not-matched
RewriteCond: input='/home/ubuntu/domain/app/preprod/' pattern='-d' => matched
pass through /home/ubuntu/domain/app/preprod/
strip per-dir prefix: /home/ubuntu/domain/app/preprod/index.php -> index.php
applying pattern '^' to uri 'index.php'
RewriteCond: input='/home/ubuntu/domain/app/preprod/index.php' pattern='-f' => matched
pass through /home/ubuntu/domain/app/preprod/index.php
这会带你去任何地方吗
---第二次编辑---
strip per-dir prefix: /home/ubuntu/domain/app/preprod/ ->
applying pattern '^' to uri ''
RewriteCond: input='/home/ubuntu/domain/app/preprod/' pattern='-f' => not-matched
RewriteCond: input='/home/ubuntu/domain/app/preprod/' pattern='-d' => matched
pass through /home/ubuntu/domain/app/preprod/
strip per-dir prefix: /home/ubuntu/domain/app/preprod/index.php -> index.php
applying pattern '^' to uri 'index.php'
RewriteCond: input='/home/ubuntu/domain/app/preprod/index.php' pattern='-f' => matched
pass through /home/ubuntu/domain/app/preprod/index.php
我真的不明白为什么调用
portail.domain.com
不会导致portail index.php
您有一些多余的条件。你的规则是这样的:
# ROUTING portail SUBDOMAIN TO portail-index.php
RewriteCond %{HTTP_HOST} ^portail\. [NC]
RewriteRule ^(index\.)?[^.]*$ portail-index.php [L,NC]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
# REROUTING EVERYTHING ELSE TO index.php
RewriteCond %{HTTP_HOST} !^portail\. [NC]
RewriteRule ^ index.php [L]
呵呵,又是你:)你能给我解释一下重写规则后会发生什么吗?我会实施你的解决方案的同时你是男人!非常感谢,先生,你真的帮了我的忙。这是非常亲切和感激的。你能不能很快地解释一下这两行代码的作用:
RewriteRule^(index\)?[^.]*$portail-index.php[L,NC]
和RewriteRule^-[L]
。我真的不明白。再次感谢你RewriteRule^-[L]
表示如果2RewriteCond
在此行上方相遇,则不执行任何操作。因此,这意味着忽略以下所有文件和目录规则。模式^(index\)?[^.]*$
表示匹配空字符串(登录页)或index.php
或index.html
或任何没有点的内容^[^.]*$
表示除点以外的任何内容。我不知道字符集开头的^
与除这些字符以外的任何内容匹配。你太棒了!