简体   繁体   中英

.htaccess config with symbolic links and index files not working as expected

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.

So something like this:

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

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

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

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

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]

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."

The -l flag references symbolic links

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM