我使用名爲SilverStripe的框架......我們目前正在將舊網站遷移到此框架中。問題在於舊網站的網址以.php或.html結尾,而在新網站中卻沒有。.htaccess重定向.php和.html請求
我需要修改第二個重寫規則,以便將請求傳送到main.php而不帶任何.html或.php擴展名。
在我目前的.htaccess我有以下規則:
# Turn off index.php handling requests to the homepage fixes issue in apache >=2.4
<IfModule mod_dir.c>
DirectoryIndex disabled
</IfModule>
SetEnv HTTP_MOD_REWRITE On
RewriteEngine On
# Enable HTTP Basic authentication workaround for PHP running in CGI mode
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Deny access to potentially sensitive files and folders
RewriteRule ^vendor(/|$) - [F,L,NC]
RewriteRule silverstripe-cache(/|$) - [F,L,NC]
RewriteRule composer\.(json|lock) - [F,L,NC]
# Process through SilverStripe if no file with the requested name exists.
# Pass through the original path as a query parameter, and retain the existing parameters.
RewriteCond %{REQUEST_URI} ^(.*)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* framework/main.php?url=%1 [QSA]
# If framework isn't in a subdirectory, rewrite to installer
RewriteCond %{REQUEST_URI} ^(.*)/framework/main.php$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . %1/install.php? [R,L]
可能的解決方法(仍在測試):
RewriteCond %{REQUEST_URI} ^(.*)\.html [OR]
RewriteCond %{REQUEST_URI} ^(.*)\.php [OR]
RewriteCond %{REQUEST_URI} ^(.*)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* framework/main.php?url=%1 [QSA]
@ 3dogo不幸的是規則沒有觸發 –
這很奇怪。它適用於我的測試。例如,如果我訪問「www.example.com/about.html」,它將重定向到「www.example.com/about」。另一個例子「www.example.com/about/team.php」將重定向到「www.example.com/about/team」。 – 3dgoo
你用什麼版本的Apache進行測試? –