【问题标题】:.htaccess rewriterule to create subdomains.htaccess rewriterule 创建子域
【发布时间】:2014-07-02 00:39:08
【问题描述】:

我想创建子域,以便我可以使用地址 test.mydomain.com。
在网上搜索和大量试验后,我在我的 .htacces 文件中得到了这个:

<IfModule mod_rewrite.c>
    Options +FollowSymLinks
    RewriteEngine on
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteCond %{HTTP_HOST} ^([^.]+)\.mydomain\.com(:80)?$
    RewriteCond %{DOCUMENT_ROOT}/subdomains/%1/$1 -f [OR]
    RewriteCond %{DOCUMENT_ROOT}/subdomains/%1/$1 -d
    RewriteRule (.*) /subdomains/%1/$1 [L]
</IfModule>

如果我在浏览器中写 http://test.mydomain.com/folder/ 一切都很好。

但如果我写 http://test.mydomain.com/folder (没有结束斜杠),它会被重定向到 http://test.mydomain.dk/subdomains/test/folder /,这是浏览器中显示的地址(我不要)

我的问题是:
如何防止重定向?
我只想在浏览器中看到 http://test.mydomain.com/folder。不管结尾的斜线如何。

我的网页文件是这样的:

.htaccess
index.php (page for mydomain.com)
\subdomains
    \test
        \hello
         index.php (page for test.mydomain.com/hello)

谢谢。

(对不起,网址中的空格。它不会让我的帖子带有链接)

【问题讨论】:

  • 没用:试试这个:app.npham.dk/test vs. app.npham.dk/test
  • 1) 我在规则中看不到任何外部重定向,因此原因可能不在规则中。 DirectorySlash On(默认值)可以进行重定向。 2) 我不确定 RewriteConds 中的 AND/OR 优先级,所以 [OR] 标志可能会破坏您的条件。 3) $1 后向引用只能在RewriteRule 中使用,所以从RewriteCond 中删除它并使用%{REQUEST_URI}。

标签: apache .htaccess mod-rewrite subdomain


【解决方案1】:

现在开始工作了:

<IfModule mod_rewrite.c>
    Options +FollowSymLinks
    RewriteEngine on

    # If requested URI does not end in "/"
    RewriteCond $1 !/$
    # add a trailing slash
    RewriteRule ^(.+)$ /$1/ [R=301,L]

    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteCond %{HTTP_HOST} ^([^.]+)\.mydomain\.com(:80)?$
    RewriteCond %{DOCUMENT_ROOT}/subdomains/%1/%{REQUEST_URI} -d [OR]
    RewriteCond %{DOCUMENT_ROOT}/subdomains/%1/%{REQUEST_URI} -f
    RewriteRule (.*) /subdomains/%1/$1 [L]
</IfModule>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-14
    • 1970-01-01
    • 1970-01-01
    • 2011-10-28
    • 1970-01-01
    • 1970-01-01
    • 2014-03-17
    相关资源
    最近更新 更多