在 .htaccess 中一起使用 mod_rewrite 和 mod_alias(重定向 301)? [英] Using mod_rewrite and mod_alias (redirect 301) together in .htaccess?

查看:22
本文介绍了在 .htaccess 中一起使用 mod_rewrite 和 mod_alias(重定向 301)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网站,其中包含一组已放入 CMS 的旧 .html 和 .php 页面.

I have a site with a set of old .html and .php pages that have been put into a CMS.

目前在 .htaccess 文件中有大约 30 个 mod_alias 重定向,格式如下:

Currently in the .htaccess file there are about 30 mod_alias redirects in the following form:

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 的形式,因此还有以下内容:

But we want to use mod_rewrite to have pretty URLs in our CMS, which will take the form http://www.example.com/pagename.php, so also have the following:

RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?page=$1

目前两者同时应用,结果:

At the moment both are being applied together, which results in:

http://www.example.com/newpage1.php?page=oldpage1.html

如何仅在 mod_alias 重定向 301 语句未进行匹配时才应用重写规则,从而发生以下情况:

How can I apply the rewrite rule only when no match has been made by the mod_alias redirect 301 statements, so that the following occurs:

http://www.example.com/oldpage1.html -> 重定向到 ->http://www.example.com/newpage1.php -> 被视为 ->http://www.example.com/index.php?page=/newpage1.php

http://www.example.com/oldpage1.html -> redirects to -> http://www.example.com/newpage1.php -> which is treated as -> http://www.example.com/index.php?page=/newpage1.php

任何提示将不胜感激?谢谢.

Any hints would be very much appreciated? Thanks.

推荐答案

我在 很棒的文章中找到了答案mod_rewrite 和 mod_alias 的解释

问题是 mod_rewrite 总是发生在 mod_alias 之前,无论它们在 .htaccess 中的放置顺序如何.这与这种情况所需的顺序相反.

The problem is that mod_rewrite always occurs before mod_alias, regardless of the order the are placed in .htaccess. This is the reverse of the order required for this situation.

诀窍是使用 RewriteRule [R=301] 而不是 redirect 301,因此对所有内容都使用 mod_rewrite 而不是将其与 mod_alias 混合使用.

The trick is to use RewriteRule [R=301] instead of redirect 301, and hence use mod_rewrite for everything instead of mixing it with mod_alias.

完整解决方案如下:

RewriteEngine on
RewriteBase /

RewriteRule ^oldpage1.html /newpage1.php [R=301,L]
RewriteRule ^oldpage2.php /newpage2.php [R=301,L]
RewriteRule ^oldpage3.php /newpage3.php [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?page=$1

这篇关于在 .htaccess 中一起使用 mod_rewrite 和 mod_alias(重定向 301)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆