【问题标题】:how to convert all subdomain request to main domain, and keep url rewrite如何将所有子域请求转换为主域,并保持 url 重写
【发布时间】:2013-10-07 05:01:33
【问题描述】:

你好,我有一个网站,有一个主域和子域.. 在 apache web 服务器和 centOS 上运行......我还有一个 url 重写机制,例如

http://subdomain.domain.com/buy-a-new-car

我想将所有子域请求重定向到主域,保持 url 重写如下:

http://domain.com/buy-a-new-car

我目前拥有的 .htaccess 代码导致:

http://domain.com/index.php?buy-a-new-car

id 喜欢去掉 (index.php?) 部分,但我是编写 .htaccess 指令的新手,并且被 REGEX 弄糊涂了

这是我当前的代码:

RewriteEngine on
RewriteBase /
Options -Indexes
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f

RewriteRule ^(.*)$ index.php?/$1

RewriteCond %{HTTP_HOST} ^subdomain.domain.com$
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]

任何帮助将不胜感激!

【问题讨论】:

    标签: regex apache .htaccess mod-rewrite


    【解决方案1】:

    您需要先进行重定向,然后在最后进行路由到index.php:

    RewriteEngine on
    RewriteBase /
    Options -Indexes -Multiviews
    
    # redirect subdomains to main domain
    RewriteCond %{HTTP_HOST} ^subdomain.domain.com$
    RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]
    
    # redirect direct accesses to index.php 
    RewriteCond %{THE_REQUEST} \ /index\.php\?/([^&\ ]+)&?([^\ ]*)
    RewriteRule ^ /%1?%2 [L,R=301]
    
    # route everything to index.php internally
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ index.php?/$1
    

    【讨论】:

      【解决方案2】:

      为什么不使用Redirect 和/或RedirectMatch 并保存Rewrite 的东西来真正重写URL?见http://httpd.apache.org/docs/current/rewrite/avoid.html

      我发现这是一种更“自我记录”的方法,它不会强迫您每次进入时都了解所有重写逻辑。

      【讨论】:

        猜你喜欢
        • 2010-10-31
        • 1970-01-01
        • 1970-01-01
        • 2011-05-25
        • 2015-07-26
        • 2017-01-26
        • 1970-01-01
        • 1970-01-01
        • 2014-04-19
        相关资源
        最近更新 更多