htaccess的配置与符号链接和索引文件工作不正常不正常、符号、索引、链接

2023-09-02 00:50:01 作者:. 转角踩到屎

像很多Web开发人员,我喜欢用的.htaccess通过一个单一入口点,将所有流量域时的要求不是存在于公共服务目录中的文件。

Like a lot of web developers, I like to use .htaccess to direct all traffic to a domain through a single point of entry when the request isn't for a file that exists in the publicly served directory.

因此​​,像这样:

RewriteEngine On
# enable symbolic links
Options +FollowSymLinks
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+) index.php [L]

这意味着,如果该请求不是一个CSS文件或图像,我的index.php获得请求,我可以选择做什么(服务于一些内容或者404)

This means if the request isn't for a css file or an image, my index.php gets the request and I can choose what to do (serve up some content or perhaps a 404)

伟大的作品,但我偶然发现了一个问题,它似乎无法帮我解决。

Works great, but I've stumbled upon an issue it can't seem to help me solve.

我的文档根目录是这样的:

My document root looks like this:

asymboliclink -> /somewhere/else
css
.htaccess
img
includes
index.php

然而,Apache不看作为一个目录到一个目录的符号链接。它传递给我的根index.php文件的要求。我想服务的链接,好像它是一个文件夹,因为它是一个链接到与它自己的index.php的文件夹。我可以通过输入其在浏览器的地址栏中访问 http://example.com/asymboliclink/index.php ,但我希望能够通过 http://example.com/asymboliclink

Yet, Apache doesn't see the symbolic link to a directory as a directory. It passes the request on to my index.php in the root. I want to serve the link as if it were a folder as it is a link to a folder with it's own index.php. I can access http://example.com/asymboliclink/index.php by typing it in the address bar of a browser, but I want to be able to access it through http://example.com/asymboliclink

我需要什么添加到我的.htaccess文件来实现这一目标?

What do I need to add to my .htaccess file to make this happen?

推荐答案

使用了 -l 开关测试的符号链接

Use the -l switch to test for a symbolic link

RewriteEngine On
# enable symbolic links
Options +FollowSymLinks
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+) index.php [L]

文档

http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html - 按Ctrl + F为将TestString视为一个路径名并测试它是否存在,并且是一个符号链接。

Documentation

http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html - ctrl+f for "Treats the TestString as a pathname and tests whether or not it exists, and is a symbolic link."