我的 *.conf 文件中有这个(我从别人那里得到这个,不确定是否有错误),我不太明白:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} mycompany.com
RewriteRule ^$ http://mycompany.com/login [L]
# we check if the .html version is here (caching)
RewriteRule ^$ index.html [QSA]
RewriteRule ^([^.]+)$ $1.html [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
# no, so we redirect to our front web controller
RewriteRule ^(.*)$ index.php [QSA,L]
我大致了解它的作用;首先,它检查传入的请求是否为mycompany.com
,如果是,则重定向到http://mycompany.com/login
。
那么,下一个要检查的条件是什么?事实上存在这个
RewriteRule ^$ index.html [QSA]
RewriteRule ^([^.]+)$ $1.html [QSA]
在我之前发生的事情RewriteRule ^$ http://mycompany.com/login [L]
让我很困惑。
有没有办法以RewriteCond
、、、RewriteRule
格式重写上述脚本?RewriteCond
RewriteRule
答案1
您的配置文件的缩进不明确。
也许下面的内容更清楚一点:
RewriteEngine on
RewriteBase /
# If the requested hostname is mycompany
# and no path segment was given,
# redirect to /login
RewriteCond %{HTTP_HOST} mycompany.com
RewriteRule ^$ http://mycompany.com/login [L]
# Add .html extension to any requested filename.
RewriteRule ^([^.]+)$ $1.html [QSA]
# Check if requested filename (now with .html
# extension) exists,
# otherwise redirect to front controller
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
我删除了
RewriteRule ^$ index.html [QSA]
因为这可以更容易地用目錄索引指示。