我有一个网站,其中包含一组已放入CMS的旧.html和.php页面 .
目前在.htaccess文件中有大约30个mod_alias重定向,格式如下:
redirect 301 /oldpage1.html http://www.example.com/newpage1.php
redirect 301 /oldpage2.php http://www.example.com/newpage2.php
redirect 301 /oldpage3.php http://www.example.com/newpage3.php
但我们希望使用mod_rewrite在我们的CMS中拥有漂亮的URL,其形式为 http://www.example.com/pagename.php
,因此也有以下内容:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?page=$1
目前两者正在一起应用,这导致:
http://www.example.com/newpage1.php?page=oldpage1.html
仅当mod_alias重定向301语句未进行匹配时,如何应用重写规则,以便发生以下情况:
http://www.example.com/oldpage1.html
- >重定向到 - > http://www.example.com/newpage1.php
- >被视为 - > http://www.example.com/index.php?page=/newpage1.php
任何提示都会非常感激吗?谢谢 .
1 回答
我找到了答案great explanation of mod_rewrite and mod_alias
问题是mod_rewrite总是出现在mod_alias之前,无论它放在.htaccess中的顺序如何 . 这与此情况所需的顺序相反 .
诀窍是使用
RewriteRule [R=301]
而不是redirect 301
,因此将mod_rewrite用于所有内容,而不是将其与mod_alias混合使用 .完整的解决方案如下: