.htaccess - 子文件夹到文件夹=子文件夹404

时间:2012-09-22 08:54:10

标签: .htaccess subdomain directory virtualhost subdirectory

很抱歉,如果标题令人困惑。

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

我想它可以用.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

2 个答案:

答案 0 :(得分:0)

尝试将规则更改为:

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]

答案 1 :(得分:0)

首先,感谢@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,并在保留URL时将其指向/ dev / folder /子文件夹。它可以使用或不使用www。

相关问题