【问题标题】:.htaccess - Subdomains to folders = subfolders 404.htaccess - 文件夹的子域 = 子文件夹 404
【发布时间】:2012-09-14 13:44:46
【问题描述】:

对不起,如果标题令人困惑。

我在本地机器上设置了一个虚拟主机。我已将 http://dev 设置为映射到我的 /htdocs/dev 文件夹。在 dnsmasq.htaccess 的帮助下,我进行了设置,以便将 .dev 的子域映射到 /htdocs/dev 内的文件夹。当我尝试访问例如http://dev/file1.htmlhttp://folder.dev/file2.html 时,这一切都很完美。访问子文件夹时会出现问题。如果我尝试访问http://folder.dev/subfolder/http://folder.dev/subfolder/file3.html,我会得到404 Object not found

我想它可以用 .htaccess 来解决,但我试过了,但还是失败了。这是我的/htdocs/dev/.htaccess 的样子:

# Default index file
  DirectoryIndex index.php

# Interpret .html files as .php scripts
  AddHandler php5-script .php .html

# Redirect subdomains to their respective folders
  RewriteEngine on
  RewriteBase /

  RewriteCond %{HTTP_HOST} !^www\.dev$ [NC]
  RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9-]+)\.dev$ [NC]
    RewriteRule !^([a-z0-9-]+)($|/) /%2%{REQUEST_URI} [PT,L]

我应该提一下,如果我尝试访问http://dev/folder/subfolder/file3.html,没有问题。

如何设置http://folder.dev/subfolder/等地址指向/htdocs/dev/folder/subfolder

【问题讨论】:

    标签: .htaccess subdomain directory virtualhost subdirectory


    【解决方案1】:

    尝试将您的规则更改为:

    RewriteCond %{HTTP_HOST} !^www\.dev$ [NC]
    RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9-]+)\.dev$ [NC]
    RewriteCond %{DOCUMENT_ROOT}/%2{REQUEST_URI} -f [OR]
    RewriteCond %{DOCUMENT_ROOT}/%2{REQUEST_URI} -d
    RewriteRule ^ /%2%{REQUEST_URI} [PT,L]
    

    【讨论】:

    • 嗨@JonLin 感谢您的回复。我需要留下还是删除这两行:RewriteEngine onRewriteBase /
    • 所以@jon,我删除了.htaccess 中的最后三行并添加了您的行。不幸的是,它不起作用。 http://folder.dev 得到一个 500 Internal Server Error 加上 Additionally, a 500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request.
    【解决方案2】:

    首先,感谢@JonLin 给我的提示。

    我已经设法用这个 .htaccess 文件解决了这个问题:

    # Ordered list of index files, if none exist, show directory listing
      DirectoryIndex index.php index.html
    
    # Interpret .html files as .php scripts
      AddHandler php5-script .php .html
    
    # Redirect subdirectories to their respective folders
      RewriteEngine on
      Options +FollowSymlinks
      RewriteBase /
    
      RewriteCond %{HTTP_HOST} !^www\.dev$ [NC]
      RewriteCond %{HTTP_HOST} ^(www\.)?(.*)\.dev(.*)?$ [NC]
      RewriteRule !^%2\.dev%3?/$ http://dev/%2%{REQUEST_URI}/ [P]
    

    Rewrite 部分的作用是采用http://folder.dev/subfolder 形式的 URL 并将其指向 /dev/folder/subfolder,同时保留 URL。无论有无 www,它都适用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-20
      • 1970-01-01
      • 2016-01-18
      • 2021-08-14
      • 1970-01-01
      • 2012-05-19
      • 2015-04-12
      • 2016-01-19
      相关资源
      最近更新 更多