【问题标题】:How to write htaccess to redirect my site?如何编写 htaccess 来重定向我的网站?
【发布时间】:2014-05-04 14:06:28
【问题描述】:

我使用 SMF 来运行我的论坛。

这是旧论坛网址 => hxxp://www.example.com/

但是现在,我将我的论坛移动到“论坛”文件夹 => hxxp://www.example.com/forum/

Google 和其他搜索引擎使用以下网址为我的网站编制索引:hxxp://www.example.com/index.php?topic=99.0

而当用户访问hxxp://www.example.com/index.php?topic=99.0时,却找不到。

因为他必须去hxxp://www.example.com/forum/index.php?topic=99.0,对吧?

所以,我想重定向 url 来自 hxxp://www.example.com/index.php?topic=99.0 转至 hxxp://www.example.com/forum/index.php?topic=99.0

我该怎么办?我试试这个,但不行。

RewriteEngine On
RewriteBase /
RewriteRule ^index.php?topic=([0-9]+).([0-9]+)$ forum/index.php?topic=$1.$2 [R]

【问题讨论】:

  • 试试^/index.php而不是^index.php

标签: php apache .htaccess mod-rewrite redirect


【解决方案1】:

取决于您放置 htaccess 的位置。如果它在 /forum/ 那么

RewriteCond %{REQUEST_URI} !^\/forum\/
RewriteRule ^index\.php$ http://%{HTTP_HOST}/forum/index.php [R]

AFAIK 查询字符串不受 RewriteRule 正则表达式的影响。 http://httpd.apache.org/docs/2.4/mod/mod_rewrite.html

【讨论】:

    【解决方案2】:

    我猜你必须转义 RewriteRule 中的点 (.) 元字符:

    RewriteRule ^index\.php?topic=([0-9]+)\.([0-9]+)$ /forum/index.php?topic=$1.$2 [R]
    

    【讨论】:

      【解决方案3】:

      希望对你有帮助

      RewriteEngine On
      RewriteCond %{REQUEST_FILENAME} -f [OR]
      RewriteCond %{REQUEST_FILENAME} -d
      RewriteRule .+ - [L]
      RewriteRule ^index.php?topic([=0-9.]+)$  old/index.php?topic=$1 [QSA,L] 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-07-17
        • 2012-11-14
        • 2016-10-13
        • 1970-01-01
        • 2016-03-17
        • 2015-05-07
        • 2022-08-02
        • 1970-01-01
        相关资源
        最近更新 更多