我有用于testing的 .htaccess
文件,这里是我的文件:
RewriteEngine On RewriteCond %{REQUEST_URI} ^/([az-]+)/?$ [NC] RewriteRule ^ %{REQUEST_SCHEME}://%{SERVER_NAME}/?title=%1 [L]
我使用ApacheLounge的 Apache / 2.4.10(Win32)
我有以下域和子域:
两者都指向相同的IP地址,现在正如我从上面的.htaccess
预期的那样:
如果我访问example.com/Article-Name/
redirect到example.com/?title=Article-Name
将是内部的 ,我将无法在我的url栏上看到?title=Article-Name
。
这是一个例外:
如果我访问m.example.com/Article-Name/
,redirect到m.example.com/?title=Article-Name
将是外部的 ,现在我在我的url中看到m.example.com/?title=Article-Name
酒吧。
所以我期待着同样的行为,如果有人有一个想法,我会非常感激。
编辑:
我已经find了解决这个问题的方法,但是我仍然对上面的问题感兴趣,关于解决scheme是使用:
RewriteRule ^ ?title=%1 [L]
请注意:这个解决scheme只需要m.example.com ,我真的不知道这个解决scheme是如何工作的,我已经发布了这个解决scheme,因为它可能有助于找出上述问题的原因,以及这个解决scheme如何工作。
编辑2(完整的.htaccess文件):
RewriteEngine On <If "%{HTTP_HOST} = 'avielfadida.ml'"> # The actual condition is really long one so I replaced it for illustration. RewriteCond %{HTTP_USER_AGENT} (iPhone|Blackberry|Android) [NC] RewriteRule ^ http://m.%{HTTP_HOST}%{REQUEST_URI} [R,L] # I have to duplicate the RewriteCond and RewriteRule both inside the <If> and <Else> RewriteCond %{REQUEST_URI} ^/([az-]+)/?$ [NC] RewriteRule ^ %{REQUEST_SCHEME}://%{HTTP_HOST}/?title=%1 [L] # The solution(the solution is not required inside this block): #RewriteRule ^ /?title=%1 [L] </If> <ElseIf "%{HTTP_HOST} = 'm.example.com'"> # I have to duplicate the RewriteCond and RewriteRule both inside the <If> and <Else> RewriteCond %{REQUEST_URI} ^/([az-]+)/?$ [NC] RewriteRule ^ %{REQUEST_SCHEME}://%{HTTP_HOST}/?title=%1 [L] # The solution(the solution is required here): # RewriteRule ^ /?title=%1 [L] DirectoryIndex /m/index.htm /m/index.php /m/index.html </ElseIf>
重要提示:完整的.htaccess文件与问题之间没有任何关系,这个更新的原因是我犯了一个小错误,无论如何,我只用问题中的3行来testing.htaccess文件。
这里是HTTPD.CONF文件
最后更新:
RewriteEngine On # The actual condition is really long one so I replaced it for illustration. RewriteCond %{HTTP_USER_AGENT} (iPhone|Blackberry|Android) [NC] RewriteCond %{HTTP_HOST} !^m\.example\.com$ [NC] RewriteRule .* http://m.example.com%{REQUEST_URI} [R,L] RewriteCond %{REQUEST_URI} ^/([az-]+)/?$ [NC] RewriteRule .* /?title=%1 [L] <If "%{HTTP_HOST} = 'm.avielfadida.ml'"> DirectoryIndex /m/index.htm /m/index.php /m/index.html </If>
你对相对path的做法是正确的
RewriteRule ^ ?title=%1 [L]
从Apache文档
绝对url
如果指定了绝对URL,则mod_rewrite会检查主机名是否与当前主机匹配。 如果是这样,scheme和主机名被剥离出来,结果path被视为URLpath。 否则,将为给定的URL执行外部redirect。 要强制外部redirect返回到当前主机,请参阅下面的[R]标志。
m.example.com
将被视为不同的主机,因此Apache执行外部redirect。 因此,为了确保只执行内部重写,您需要像使用相对path一样。
你也可以从.htaccess文件中删除很多东西。 我认为这应该做你正在寻找的东西。
RewriteEngine On RewriteCond %{HTTP_USER_AGENT} (iPhone|Blackberry|Android) [NC] RewriteCond %{HTTP_HOST} !^m\.example\.com$ [NC] RewriteRule .* http://m.exmaple.com%{REQUEST_URI} [R,L] RewriteCond %{REQUEST_URI} ^/([az-]+)/?$ [NC] RewriteRule .* /?title=%1 [L]