我有一个奇怪的案例。我只想将所有旧的.html页面重定向到相同的.php页面。但是我认为应该有效的代码不起作用,因为它最终会重定向到完整的文件系统路径而不是 URL,从而获得 404。
起始网址是这种
dev.example.com/site-dir/my-page.html
或(本地,相同的结果(
http://localhost:5757/site-dir/my-page.html
当前的 .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*).html$ $1.php [R=301]
这将输出 404:
The requested URL /Users/[my user]/Documents/[my dirs]/my-page.php was not found on this server.
为什么?
我目前正在通过MAMP PRO使用本地apache。
已解决:我必须添加基本目录:
RewriteEngine On
RewriteBase /my-base-dir/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*).html?$ $1.php [R=301,L] ### all x.html to x.php
RewriteRule ^([a-z-]+)/?$ $1.php [L] ### all paths without extensions to .php (pretty urls)