【问题标题】:Redirect to another folder重定向到另一个文件夹
【发布时间】:2011-03-29 18:13:03
【问题描述】:

我读到了这个.htaccess rewrite to redirect root URL to subdirectory,我正在努力实现同样的目标。

得票最多的解决方案是:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
RewriteRule ^$ store [L]

现在,看看 cmets,它似乎运行良好。 问题是我不想硬编码任何路径(而是在“www.example.com”上方提供)。我希望我的.htaccessprojectname/ 内重定向到projectname/public/,尽管真正的服务器主机是什么。因此,如果我将projectname/ 放在www.pinco.com 的根服务器中,它会重定向到www.pinco.com/projectname/public/,但它会显示www.pinco.com/projectname/

我怎样才能做到这一点?

【问题讨论】:

    标签: php .htaccess mod-rewrite url-rewriting


    【解决方案1】:

    您找到的示例实际上是在做两件不同的事情。

    # This is actually just redirecting all http://example.com/somepage/
    # to http://www.example.com/somepage/, to ensure all URLs have the www.
    RewriteCond %{HTTP_HOST} ^example\.com$
    RewriteRule (.*) http://www.example.com/$1 [R=301,L]  
    

    第二次重写将帮助你实现你想要做的事情。

    # This redirect all requests for http://example.com -> http://example.com/newdir
    # If you are looking to redirect the request, so the URL contains directory name,
    # you can change the [L] to [R=301,L]
    RewriteRule ^$ /newdir [L]
    
    # If your concerned about direct access to a particular page without the sub-dir 
    # you will want to add something like this
    RewriteCond %{REQUEST_URI} !^/newdir
    RewriteRule (.*) /newdir$1 [R=301,L]
    

    因此,在这种情况下,您不必关心应用程序运行所在的域。

    【讨论】:

      【解决方案2】:

      尝试添加这一行,然后:

      RewriteBase /projectname
      

      然后你留在项目中。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-02-01
        • 2013-04-01
        • 1970-01-01
        • 2015-03-18
        • 1970-01-01
        相关资源
        最近更新 更多