【问题标题】:htaccess redirect from any subdomain to another domain and from directory to query URLhtaccess 从任何子域重定向到另一个域,从目录重定向到查询 URL
【发布时间】:2011-04-17 15:48:49
【问题描述】:

我试图使用 htaccess 将任何子域和一个域的根重定向到另一个域。 例子

anysub.mysite.com 或 mysite.com 重定向到 www.anotheriste.com

还有 mysite.com/stuffhere 到 www.anothersite.com/feed?v=stuffhere

因此,任何不是子目录的东西都会被重定向到另一个域,而任何属于子目录的东西都会被重定向到带有查询字符串的 URL。好像拿不到一个重写规则覆盖另一个。

编辑: 这就是我要工作的地方

Options +FollowSymlinks 
RewriteEngine on
RewriteRule ^$ http://www.site.com [R=301,L]

RewriteCond  %{REQUEST_FILENAME} !-f
RewriteCond  %{REQUEST_FILENAME} !-d
RewriteCond  %{REQUEST_URI} ^/*\/?
RewriteRule ^(.*)$ http://www.site.com/feed?v=$1 [R=301,L]

【问题讨论】:

    标签: .htaccess mod-rewrite redirect


    【解决方案1】:

    尝试将这些规则放入您的 .htacccess 文件中:

    RewriteEngine on
    Options +FollowSymlinks -MultiViews
    
    # handles http redirect sub-dir
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteCond %{SERVER_PORT} =80
    RewriteCond %{HTTP_HOST} ^(.+\.)?mysite\.com$ [NC] 
    RewriteRule ^/?(.*)$ http://www.anotheriste.com/feed?v=$1 [R=301,L,QSA,NE]
    
    # handles http redirect non sub-dir
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{SERVER_PORT} =80
    RewriteCond %{HTTP_HOST} ^(.+\.)?mysite\.com$ [NC] 
    RewriteRule ^/?(.*)$ http://www.anotheriste.com/$1 [R=301,L,QSA,NE]
    
    # handles https redirect sub-dir
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteCond %{SERVER_PORT} =443
    RewriteCond %{HTTP_HOST} ^(.+\.)?mysite\.com$ [NC] 
    RewriteRule ^/?(.*)$ https://www.anotheriste.com/feed?v=$1 [R=301,L,QSA,NE]
    
    # handles https redirect non sub-dir
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{SERVER_PORT} =443
    RewriteCond %{HTTP_HOST} ^(.+\.)?mysite\.com$ [NC] 
    RewriteRule ^/?(.*)$ https://www.anotheriste.com/$1 [R=301,L,QSA,NE]
    

    R=301 将重定向到 https 状态 301

    L 将制定最后一条规则

    NE 用于不转义查询字符串

    QSA 将附加您现有的查询参数

    $1 是您的 REQUEST_URI

    【讨论】:

    • 他感谢您的回答,但它并没有按预期工作。原因是,我忽略了目录不是真实的。所以 mysite.com/stuffhere 不是一个实际的目录。我正在做的一部分是短 URL,因此这些目录实际上并不存在。在做了很多搜索之后,我想出了如何完成我需要的东西。我已经编辑了我的帖子以显示我要工作的内容。我相信如果它们是真正的目录,你的会很好用。谢谢你。
    • 是的,这是正确的,我的答案是基于实际目录的存在。真的很高兴你把它变成了一个可行的解决方案。
    猜你喜欢
    • 2014-09-11
    • 1970-01-01
    • 2016-03-26
    • 2011-04-29
    • 2010-11-22
    • 2023-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多