AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 628240
Accepted
Aviel Fedida
Aviel Fedida
Asked: 2014-09-13 22:10:20 +0800 CST2014-09-13 22:10:20 +0800 CST 2014-09-13 22:10:20 +0800 CST

Apache,htaccess,url重写

  • 772

我有用于测试.htaccess的文件,这是我的文件:

RewriteEngine On
RewriteCond %{REQUEST_URI} ^/([a-z-]+)/?$ [NC]
RewriteRule ^ %{REQUEST_SCHEME}://%{SERVER_NAME}/?title=%1 [L]

我正在使用来自ApacheLounge的Apache/2.4.10 (Win32)

我有以下域和子域:

  • 例子.com
  • m.example.com

两者都指向相同的 IP 地址,正如我从上面所期望的那样.htaccess:

如果我访问example.com/Article-Name/重定向example.com/?title=Article-Name将是internal,我将无法?title=Article-Name在我的 url 栏上看到。

这是一个例外:

如果我访问m.example.com/Article-Name/重定向m.example.com/?title=Article-Name将是外部的,现在我m.example.com/?title=Article-Name在我的 url 栏中看到。

所以我期待同样的行为,如果有人有想法,我会非常感激。

编辑:

我已经找到了问题的解决方案,但我仍然对上述问题感兴趣,关于解决方案是使用:

RewriteRule ^ ?title=%1 [L]

请注意:这个解决方案只需要m.example.com,我真的不知道这个解决方案是如何工作的,我发布了这个解决方案,因为它可能有助于找到上述问题的原因,以及这个解决方案是如何工作的.

编辑 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} ^/([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>

重要说明:完整的 .htaccess 文件与问题之间没有关系,此更新的原因是我犯了一个错误的可能性很小,无论如何我只用问题中的 3 行测试了 .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} ^/([a-z-]+)/?$ [NC]
RewriteRule .* /?title=%1 [L]


<If "%{HTTP_HOST} = 'm.avielfadida.ml'">
    DirectoryIndex /m/index.htm /m/index.php /m/index.html
</If>
.htaccess
  • 1 1 个回答
  • 1486 Views

1 个回答

  • Voted
  1. Best Answer
    Vivek Thomas
    2014-09-16T07:40:35+08:002014-09-16T07:40:35+08:00

    您使用相对路径的方式是正确的

    RewriteRule ^ ?title=%1 [L]
    

    来自Apache 文档

    绝对网址

    如果指定了绝对 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]
    
    • 5

相关问题

  • apache重写以将文件夹分配给域

  • Apache conf 或 .htaccess 规则将 404 重定向到特定文件类型的另一个位置

  • 如何让 Apache2 重定向到子目录

  • .htaccess - 删除所有 cookie

  • 我的 .htaccess 文件是什么意思?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve