【问题标题】:Rewrite directory to subdomain将目录重写为子域
【发布时间】:2012-08-29 17:38:17
【问题描述】:

我有一个包含两个子域的页面:cw.sidenote.hucellwars.sidenote.hu,都指向 sidenote.hu/cellwars/

当我访问 cw.sidenote.hucellwars.sidenote.hu 时,我希望实现的 URL 更改为/保持 cellwars.sidenote.hu 格式,而不是更改为 sidenote.hu/cellwars/

这是我目前在我的.htaccess 文件中的内容:

RewriteEngine on

# Some hosts require a rewritebase rule, if so, uncomment the RewriteBase line below. If you are running from a subdirectory, your rewritebase should match the name of the path to where stacey is stored.
# ie. if in a folder named 'stacey', RewriteBase /stacey
#RewriteBase /cellwars

ErrorDocument 404 /404.html

# Rewrite any calls to *.html, *.json, *.xml, *.atom, *.rss, *.rdf or *.txt if a folder matching * exists
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !public/
RewriteCond %{DOCUMENT_ROOT}/public/$1.$2 !-f
RewriteRule (.+)\.(html|json|xml|atom|rss|rdf|txt)$ $1/ [L]

# Add a trailing slash to directories
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.)
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ([^/]+)$ $1/ [L]

# Rewrite any calls to /* or /app to the index.php file
RewriteCond %{REQUEST_URI} /app/$
RewriteRule ^app/ index.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ index.php?$1 [L]

# Rewrite any file calls to the public directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !public/
RewriteRule ^(.+)$ public/$1 [L]

这些是我尝试使用的重写规则,但似乎这会使网站陷入无限的重写循环:

# cw.sidenote.hu -> cellwars.sidenote.hu
RewriteCond %{HTTP_HOST} ^cw.sidenote.hu$
RewriteRule ^(.*)$ http://cellwars.sidenote.hu/ [R=301,L]

# sidenote.hu/cellwars/* -> cellwars.sidenote.hu/*
RewriteCond %{REQUEST_URI} ^/cellwars/?(.*)$
RewriteRule ^(.*)$ http://cellwars.sidenote.hu/%1 [R=301,L]

这是我从 Chrome 得到的结果:

This webpage has a redirect loop
The webpage at http://cellwars.sidenote.hu/ has resulted in too many redirects. Clearing your cookies for this site or allowing third-party cookies may fix the problem. If not, it is possibly a server configuration issue and not a problem with your computer.
Here are some suggestions:
    Reload this webpage later.
    Learn more about this problem.
Error 310 (net::ERR_TOO_MANY_REDIRECTS): There were too many redirects.

谁能帮帮我?提前致谢!

【问题讨论】:

    标签: .htaccess rewrite subdomain


    【解决方案1】:

    尝试用这个替换您的 htaccess 文件内容(我没有更改任何超出 ErrorDocument 404 /404.html 行的内容):

    RewriteEngine on
    
    # Some hosts require a rewritebase rule, if so, uncomment the RewriteBase line below. If you are running from a subdirectory, your rewritebase should match the name of the path to where stacey is stored.
    # ie. if in a folder named 'stacey', RewriteBase /stacey
    RewriteBase /
    
    # cw.sidenote.hu -> cellwars.sidenote.hu
    RewriteCond %{HTTP_HOST} ^cw\.sidenote\.hu$
    RewriteRule ^(.*)$ http://cellwars.sidenote.hu/$1 [R=301,L]
    
    # sidenote.hu/cellwars/* -> cellwars.sidenote.hu/*
    RewriteCond %{HTTP_HOST} ^(www\.)?sidenote\.hu$
    RewriteRule ^cellwars/?(.*)$ http://cellwars.sidenote.hu/$1 [R=301,L]
    
    ErrorDocument 404 /404.html
    
    # Rewrite any calls to *.html, *.json, *.xml, *.atom, *.rss, *.rdf or *.txt if a folder matching * exists
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_URI} !public/
    RewriteCond %{DOCUMENT_ROOT}/public/$1.$2 !-f
    RewriteRule (.+)\.(html|json|xml|atom|rss|rdf|txt)$ $1/ [L]
    
    # Add a trailing slash to directories
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !(\.)
    RewriteCond %{REQUEST_URI} !(.*)/$
    RewriteRule ([^/]+)$ $1/ [L]
    
    # Rewrite any calls to /* or /app to the index.php file
    RewriteCond %{REQUEST_URI} /app/$
    RewriteRule ^app/ index.php [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/$ index.php?$1 [L]
    
    # Rewrite any file calls to the public directory
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !public/
    RewriteRule ^(.+)$ public/$1 [L]
    

    【讨论】:

    • 谢谢,但没用。如果我输入子域:cw.sidenote.hucellwars.sidenote.hu,它会更改为 sidenote.hu/cellwars/,并且在我无法访问网站上的任何页面之后。
    猜你喜欢
    • 2016-11-20
    • 1970-01-01
    • 1970-01-01
    • 2012-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-29
    相关资源
    最近更新 更多