我需要一些关于正确编写.htaccess
文件的帮助。我确实尽了最大努力,但它并不总是按预期工作。我想要实现的是:
- 首先,如果浏览器请求
favicon.ico
,它立即得到 404,没有异常,没有重写/重定向(我当前的代码仍然重写它,然后返回 404), - 如果子域是
admin.
,则将流量重写到/admin/
文件夹(保留admin.
在 URL 中),否则重写到www.
- 检查 URL 是否
www.
在域前面,即使在任何“子文件夹”(例如:domain.com/en/sub1/sub2/)上,否则重定向, - 检查是否选择了语言 (en|sl),否则选择默认
sl
并将其他请求重定向到根目录中的 index.php(包含语言代码,例如:www.domain.com/en/example/#selection,?a= b 不需要发,我不会用)
当前.htaccess
:
Options +FollowSymlinks -Indexes
RewriteEngine on
// this works somewhat (still rewrites 2 times... I can see it in apache log)
RedirectMatch 404 favicon.ico
// this doesn't work at all
Redirect 404 /favicon.ico
RewriteCond %{HTTP_HOST} ^admin\. [NC]
RewriteRule ^(.*)$ /admin/$1 [L]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
# Check if lang. code is provided otherwise select sl
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/(sl|en)/ [NC]
RewriteRule ^(.*)$ /sl/$1 [R,L]
# Add trailing slash if not found
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ /$1/ [R,L]
# Process virtual links/directories
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/?$1 [QSA,L]
问题:
- 访问时
admin.domain.com
我得到500 Internal Server Error
, favicon.ico
在第一次实例中不返回 404,- 其他一切正常,但如果您对如何改进它有建议,我将不胜感激。
解决方案:
我删除了RedirectMatch
规则并使用Redirect 404 /favicon.ico
,但除此之外,我几乎必须在任何地方添加RewriteCond %{REQUEST_FILENAME} !favicon.ico
才能获得正确的 404 响应(没有任何重写/重定向)。
关于 admin 子域:我留下了完全相同的规则,但我.htaccess
在 /admin 文件夹中创建了额外的规则,用于整理到达那里的流量。