内部重定向重写规则无法正常工作

时间:2016-01-26 15:39:54

标签: apache .htaccess redirect mod-rewrite

我相信我正在寻找的解决方案非常简单,但我花了将近一天时间寻找它并没有成功,所以我投降了。

我想将所有请求从package mor; import java.util.Scanner; public class Mor { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("Type 1 for text to morse, 0 from morse to text."); String myChoice = input.nextLine(); System.out.println("Type what you want to decode/encode "); String userMessage = input.nextLine(); System.out.println(""); if("1".equals(myChoice)){ System.out.println(toMorse(userMessage));} else{ System.out.println(fromMorse(userMessage)); } } public static String fromMorse(String userMessage) { String s = userMessage; System.out.printf("Message: %s\n", s); // string with decoded message String message = ""; // sample alphabet string String alphabet = "abcdefghijklmnopqrstuvwxyz"; String[] morseAlphabet = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."}; // separate on string letters String[] letters = s.split(" "); // iteration morse letters for (String morseAlph : letters) { char alphabetLetter = '?'; int index = -1; for (int i = 0; i < morseAlphabet.length; i++) { if (morseAlphabet[i].equals(morseAlph)) index = i; } if (index >= 0) // letter found alphabetLetter = alphabet.charAt(index); message += alphabetLetter; } return message; } public static String toMorse(String userMessage) { String s = userMessage; System.out.printf("Message: %s\n", s); String message = ""; String alphabet = "abcdefghijklmnopqrstuvwxyz"; String[] morseAlphabet = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."}; for (char alphabetLetter : message.toCharArray()) { int index = -1; String letter = " "; for (int i = 0; i < alphabet.length(); i++) { if (alphabet.charAt(i) == alphabetLetter) { index = i; } } if (index >=0) { letter = morseAlphabet[index]; } message += letter + " "; } System.out.printf("Message: %s\n", message); return message; } } 重定向到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] 时的日志结果:

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 重放一大堆重写规则。 我不明白为什么第二次传递再次与internal redirect规则不符。

我尝试用portail.domain.com标志替换L标志,但无济于事。结果完全一样。我还尝试了几种重写规则的改编,但这些尝试都没有奏效。我花了几个小时阅读Apache rewrite_mod文档,所以我相信我对模块的理解非常好,但是在这种情况下无法使它正常工作。

任何关于它为什么不起作用或更好的建议:如何使这项工作受到欢迎。

---编辑---

非常感谢你的帮助@Anubhava。以下是我实现END文件版本后得到的日志:

.htaccess

这会带你到任何地方吗?

---第二次编辑---

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

1 个答案:

答案 0 :(得分:1)

你有一些多余的条件。你有这样的规则:

# 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]