.htaccess - 子文件夹到子文件夹=子文件夹404
问题描述:
对不起,如果标题混淆。.htaccess - 子文件夹到子文件夹=子文件夹404
我在本地机器上设置了一个虚拟主机。我已将http://dev
设置为映射到我的/htdocs/dev
文件夹。从dnsmasq和.htaccess的一些帮助,我已经设置它,以便它将子域.dev
映射到/htdocs/dev
内的文件夹。当我尝试访问时,这一切都很完美,例如,http://dev/file1.html
或http://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
?
答
首先,感谢送给@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]
怎么看的改写部确实是需要一个URL中的http://folder.dev/subfolder形式和它指向到/ dev /文件夹/子文件夹,而保存网址。它可以使用或不使用www。
答
试着改变你的规则:
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 on'和'RewriteBase /' –
@ТомицаКораћLeave them –
So @jon,我删除了我的.htaccess中的最后三行并添加了行。不幸的是,它不工作。 'http:// folder.dev'获得'500内部服务器错误'加''另外,尝试使用ErrorDocument处理请求时遇到500内部服务器错误错误。 –