【问题标题】:htaccess redirect directory to query stringhtaccess 重定向目录到查询字符串
【发布时间】:2015-11-26 08:42:04
【问题描述】:

我正在尝试找到重定向这些网址的方法:

http://example.com/some-directory-name/
http://example.com/some-directory-name

到这个网址:

http://example.com?post=some-directory-name

到目前为止,我得到了这个:

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule ^(.*)$ http://example.com?post=$1 [L,R=302]

但这一切只是将我重定向到这个 url 并导致重定向循环:

http://example.com/?post=

我以为 [L] 标签会导致规则不再被应用?还有,为什么要在查询字符串前加一个斜杠,把 $1 部分扔掉?

任何帮助将不胜感激!

【问题讨论】:

  • "为什么要添加斜线" - 浏览器 添加斜线以使 URL 有效。 URL-path 至少应该是/,而不是empty。

标签: regex apache .htaccess mod-rewrite redirect


【解决方案1】:

当查询字符串已经存在时,您需要使用RewriteCond 来停止重定向:

Options +FollowSymLinks
RewriteEngine On

RewriteCond %{QUERY_STRING} !(?:^|&)post= [NC]
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.+)$ http://%{HTTP_HOST}?post=$1 [L,R=302,QSA]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-26
    • 1970-01-01
    • 2015-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-27
    相关资源
    最近更新 更多