【问题标题】:Apache htaccess multiple rewritecondApache htaccess 多个 rewritecond
【发布时间】:2011-09-30 22:27:19
【问题描述】:

我在我的 Web 服务器上的 htaccess 文件中写入 rewritecond 时遇到问题。如果有人可以帮助我,那对我很有帮助。

我想要完成的是这两件事: 1.将所有对http://domain.com的请求重定向到http://www.domain.com 2. 如果存在,则从 URL 中重定向/删除 index.html 部分。

我当前的 htaccess 设置是:

Options +FollowSymlinks
RewriteEngine on

RewriteCond %{http_host} ^example.com [nc]
RewriteRule ^(.*)$ http ://www. example. com/$1 [R=301,NC]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.html\ HTTP/
RewriteRule ^(.*)index\.html$ [b]http :// www. example. com[/b]/$1 [R=301,L]

发生的情况是,当 URL 包含 index.html 时,它基本上给出了 404 页面。不确定重定向在哪里循环。有人插话以阐明这个问题吗?

提前致谢。

【问题讨论】:

    标签: apache .htaccess redirect web webserver


    【解决方案1】:

    第一个规则已测试,第二个未测试但应该有效。 :)

    Options +FollowSymlinks
    RewriteEngine on
    
    # redirect domain.com to www.domain.com
    RewriteCond %{HTTP_HOST} ^([a-z0-9\-]+)\.([a-z0-9\-]+)$ [NC]
    RewriteRule ^/?(.*) http://www.%1.%2/$1 [R=301,L]
    
    RewriteRule ^/?index\.html(\?.*)? /$1 [R=301]
    

    【讨论】:

    • 完美!我进行了一些更改以删除任何级别的所有 index.html,但这现在效果很好:
      Options +FollowSymlinks
      RewriteEngine on
      RewriteCond %{HTTP_HOST} ^([a-z0-9 \-]+)\.([a-z0-9\-]+)$ [NC]
      重写规则 ^/?(.*) %1.%2/$1 [R=301,L]
      重写规则^(.*)/?index\.html(\?.*)? /$1 [R=301]
    猜你喜欢
    • 2013-07-17
    • 1970-01-01
    • 2011-06-04
    • 1970-01-01
    • 2020-05-07
    • 1970-01-01
    • 2011-11-05
    • 1970-01-01
    • 2023-01-25
    相关资源
    最近更新 更多