【问题标题】:complex .htaccess rules复杂的 .htaccess 规则
【发布时间】:2011-12-21 08:03:21
【问题描述】:

在编写一些 .htaccess 重写规则方面寻求专家的帮助。这是我想要完成的任务:

#) 向用户显示的用户请求作为对服务器的实际请求 1) www.example.com* example.com* example.com* 2) example.com example.com example.com/index.php 3) example.com/index.php example.com example.com/index.php 4) example.com/mypage example.com/mypage example.com/index.php?p=mypage 5) m.example.com m.example.com example.com/mobile.php 6) m.example.com/index.php m.example.com example.com/mobile.php 7) m.example.com/mobile.php m.example.com example.com/mobile.php 8) m.example.com/mypage m.example.com/mypage example.com/mobile.php?p=mypage

这是我到目前为止所做的工作。它负责第 1、3 和 4 行。我一开始添加移动设备,我就迷路了。

重写引擎开启 RewriteBase / RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC] 重写规则 ^ http://%1%{REQUEST_URI} [R=301,L] RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC] RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,L] 重写规则 ^([a-z\-]+)$ /index.php?p=$1 [L]

我花了太多时间试图弄清楚这一点,而且我总是陷入死胡同。我不断地让它的一部分工作,但其他部分就坏了。任何帮助将不胜感激。

谢谢!

【问题讨论】:

    标签: regex apache .htaccess


    【解决方案1】:

    我想您必须先添加移动规则,以免意外触发其他规则。

    RewriteEngine On
    
    # 6,7
    RewriteCond %{HTTP_HOST} ^m\. [NC]
    RewriteRule ^\/(index|mobile)\.php$ http://m.example.com/ [R=301,L]
    
    # 5,8
    RewriteCond %{HTTP_HOST} ^m\. [NC]
    RewriteRule ^([a-z\-]+)$ /mobile.php?p=$1 [L]
    
    # 1
    RewriteCond %{HTTP_HOST} ^www\. [NC]
    RewriteRule (.*) http://example.com$1 [R=301,L]
    
    # 3 (and example.com/mobile.php)
    RewriteRule ^\/(index|mobile)\.php$ http://example.com/ [R=301,L]
    
    # 2,4
    RewriteRule ^\/([a-z\-]+)$ /index.php?p=$1 [L]
    

    【讨论】:

      猜你喜欢
      • 2019-08-09
      • 1970-01-01
      • 2010-12-07
      • 2012-05-13
      • 1970-01-01
      • 2017-08-05
      • 1970-01-01
      • 1970-01-01
      • 2015-05-22
      相关资源
      最近更新 更多