【问题标题】:Need some assistance with redirects in .htaccess.htaccess 中的重定向需要一些帮助
【发布时间】:2017-04-17 11:04:53
【问题描述】:

我的问题是非 HTTPS 请求和 WWW 请求都重定向到 Main_Page,当请求的 url 是根域时这很好,但如果请求是文章、特殊页面等则不是。

示例 1: http://example.com/wiki/Articlename 重定向到 https://example.com/wiki/Main_Page,但我想重定向到 https:// 示例。 com/wiki/文章名称

示例 2: https://www.example.com/wiki/Articlename 重定向到 https://example.com/wiki/Main_Page,但我想重定向到 https:// example.com/wiki/Articlename

这是我在 .htaccess 中的设置:

<IfModule rewrite_module>
RewriteEngine On
RewriteBase /
Options +FollowSymlinks

# Force HTTP to HTTPS/SSL
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://example.com/$1 [R,L]

# Redirect Example.com to Example.com/wiki/Main_Page
RewriteCond %{HTTP_HOST} example\.com [NC]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ /wiki/$1 [PT,L]

# Short url for wiki pages
RewriteRule ^/?wiki(/.*)?$ %{DOCUMENT_ROOT}/w/index.php [PT,L]

# Permanent redirect www url to non-www
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule (.*) http://example.com/$1 [L,R=301]
</IfModule>    

在共享主机(Debian 8 Jessie、Apache、PHP 5.6)上使用 MediaWiki,因此不能选择 httpd.conf。 Example.com 仅服务于 MediaWiki,这就是域被重定向到 https://example.com/wiki/Main_Page(默认)的原因。

知道我做错了什么吗?

编辑:在 .htaccess 中将“# Force HTTP to HTTPS/SSL”移动到顶部后,HTTPS 重写现在可以工作,但 WWW-rewrite 仍然无法工作。代码已更新。

【问题讨论】:

    标签: apache .htaccess redirect mod-rewrite


    【解决方案1】:

    我认为你的问题在于你在 RewriteRule 之后使用“L” - http://httpd.apache.org/docs/current/rewrite/flags.html#flag_l

    它会导致规则处理停止,而您的逻辑似乎需要执行多个规则

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多