开发者

mod_rewrite: no access to real files and directories

开发者 https://www.devze.com 2023-01-04 02:32 出处:网络
I use mod_rewrite/.htaccess for pretty URLs. I forward all the requests to my index.php, like this: RewriteRule ^/?([a-zA-Z0-9/-]+)/?$ /index.php [NC,L]

I use mod_rewrite/.htaccess for pretty URLs. I forward all the requests to my index.php, like this:

RewriteRule ^/?([a-zA-Z0-9/-]+)/?$ /index.php [NC,L]

The index.php then handles the requests.

I'm also using this condition/rule to eliminate trailing slashes (or rather rewrite them to the URL without a trailing slash, with a 301 redirect; I'm do开发者_StackOverflow中文版ing this to avoid duplicate content and because I like no trailing slashes better):

RewriteCond %{HTTP_HOST} !^\.localhost$ [NC]
RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [R=301,L]

This works well, except that I now get an infinite loop when trying to access a (real) directory (the rewrite rule removes the trailing slash, the server adds it again, ...).

I solved this by setting the DirectorySlash directive to Off:

DirectorySlash Off

I don't know how good this solution is, I don't feel too confident about it tbh.

Anyway, what I'd like to do is completely ignore "real" files and directories, since I don't need them and I only use pretty URLs with "virtual" files/directories anyway. This would allow me to avoid the DirectorySlash workaround/hack too.

Is this possible?

Thanks!


This is because you overwrite the file and folder request, too. You should add a new RewriteCond before the rule, which tells the server it should only rewrite non-existing items.

The code:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

I hope this helps.

0

精彩评论

暂无评论...
验证码 换一张
取 消