【问题标题】:Rewrite whole URL with .htaccess用 .htaccess 重写整个 URL
【发布时间】:2016-03-24 12:54:13
【问题描述】:

我有一个带有如下子域的 URL:

test.my-site.com/

假设我想向其中添加论坛部分,该部分适用于:

test.my-site.com/forums/

这可行,但我现在想重写 URL,以便每次有人访问论坛部分而不是 test.my-site.com/forums 他在 URL 中看到 forums.my-site.com/

我正在尝试通过.htaccess 这样做:

RewriteRule test.my-site.com/forums/(.*)$ forums.my-site.com/$1 [R=302,NE,L]

但是,这绝对没有效果。有什么建议吗?

【问题讨论】:

    标签: apache .htaccess url mod-rewrite


    【解决方案1】:

    用途:

    RewriteEngine on
    # Redirect to forums subdomain
    RewriteCond %{HTTP_HOST} ^test\.my-site\.com$ [NC]
    RewriteRule ^forums/(.*)$ http://forums.my-site.com/$1 [NC,R=302,NE,L]
    
    # Rewrite from forums subdomain to forums folder
    RewriteCond %{HTTP_HOST} ^forums\.my-site\.com$ [NC]
    RewriteRule ^(.*)$ /forums/$1 [NE,L]
    

    【讨论】:

    • 即使您的编辑似乎仍然无法正常工作。 URL 根本不会改变。
    • 您是否配置了您的服务器和 DNS 以使用 forums 子域?当您尝试使用 http://forums.my-site.com 时,这项工作有效吗?
    • 我猜不会。 forums.my-site.com 子域根本不存在。如果我创建它并使用上面的 .htaccess,我是否需要在该文件夹中输入一些内容?
    • 根据服务器配置,通常不需要使用.htaccess。因为子域被用作另一个站点。但是你仍然可以像我一样提供一个,如果用户没有看到子域不一样(并且你在链接中使用/forums/
    • 做你想做的事的正确方法就是 Croises 所说的。您需要先在 DNS 中创建一个论坛子域并指向它,而不是尝试做一些事情。
    【解决方案2】:

    您可以创建一个子域论坛并使用 public_html/forums 作为它的目录根。在子域目录中创建 index.html 文件并使用以下 html 标签:

    <html>
    <head>
    <title>you title goes here</title>
    </head><frameset rows="100%" scrolling="yes" border="0">
    <frame src="test.my-site.com/forums">
    </frameset></html>
    

    试试看有什么问题告诉我

    【讨论】:

    • 我认为这有问题。假设我正在浏览 test.my-site.com/forums/topic_name/post_name。如果我在框架中使用您的建议,我的 URL 将始终显示 forums.my-site.com 而我希望它显示 forums.my-site.com/topic_name/post_name
    【解决方案3】:

    这是我的建议

    RewriteRule ^mySite.com/([forum|blog])$ $1.mySite.com/ [QSA,L] 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-12
      • 2016-08-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多