Avi*_*ida 2 mod-rewrite .htaccess apache-2.4
我有.htaccess我使用的文件进行测试,这是我的文件:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/([a-z-]+)/?$ [NC]
RewriteRule ^ %{REQUEST_SCHEME}://%{SERVER_NAME}/?title=%1 [L]
Run Code Online (Sandbox Code Playgroud)
我正在使用来自ApacheLounge 的Apache/2.4.10 (Win32)
I have the following domain and sub domain:
Both point to the same ip address, now as I expected from the above .htaccess:
if I access example.com/Article-Name/ the redirection to example.com/?title=Article-Name will be internal, I won't be able to see ?title=Article-Name on my url bar.
Here is the exception:
If I access m.example.com/Article-Name/ the redirection to m.example.com/?title=Article-Name will be external and now I see m.example.com/?title=Article-Name in my url bar.
So I was expecting the same behavior, if anyone have an idea I will be very thankful.
EDIT:
I have found a solution for the problem but I still interesting about the above problem, about the solution is to use:
RewriteRule ^ ?title=%1 [L]
Run Code Online (Sandbox Code Playgroud)
Please notice: this solution is only needed for m.example.com, I really have no idea how this solution is even working, I have posted this solution because it may help finding the cause for above problem, and how this solution is even working.
EDIT 2(the full .htaccess file):
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} ^/([a-z-]+)/?$ [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} ^/([a-z-]+)/?$ [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>
Run Code Online (Sandbox Code Playgroud)
Important note: there is no relation between the full .htaccess file and the problem, the reason for this update is for the small chance I have made a mistake, anyway I have tested the .htaccess file with only the 3 lines from the question.
Last update:
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} ^/([a-z-]+)/?$ [NC]
RewriteRule .* /?title=%1 [L]
<If "%{HTTP_HOST} = 'm.avielfadida.ml'">
DirectoryIndex /m/index.htm /m/index.php /m/index.html
</If>
Run Code Online (Sandbox Code Playgroud)
您对相对路径的处理方式是正确的
RewriteRule ^ ?title=%1 [L]
Run Code Online (Sandbox Code Playgroud)
绝对网址
如果指定了绝对 URL,mod_rewrite 会检查主机名是否与当前主机匹配。如果是,则方案和主机名将被删除,结果路径被视为 URL 路径。否则,将对给定 URL 执行外部重定向。要强制外部重定向回当前主机,请参阅下面的 [R] 标志。
m.example.com将被视为不同的主机,因此 Apache 执行外部重定向。因此,为了确保只执行内部重写,您需要像之前一样使用相对路径。
您还可以从 .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} ^/([a-z-]+)/?$ [NC]
RewriteRule .* /?title=%1 [L]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1424 次 |
| 最近记录: |