- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个网站,其中包含一组已放入 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
任何提示将不胜感激?谢谢。
最佳答案
我在 great explanation of mod_rewrite and mod_alias 中找到了答案
问题在于 mod_rewrite 始终发生在 mod_alias 之前,无论它们在 .htaccess 中的放置顺序如何。这与这种情况所需的顺序相反。
诀窍是使用 RewriteRule [R=301]
而不是 redirect 301
,因此对所有内容都使用 mod_rewrite,而不是将其与 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 - 在 .htaccess 中一起使用 mod_rewrite 和 mod_alias(重定向 301)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4856193/
我有两个服务器: Fedora running Apache/2.2.14 (old server) CentOs running Apache/2.2.3 (new server) 我已经使用这个重
我为一本杂志创建了一个 django 站点,它可以在 mag.org/django-site 中找到。旧站点仍在 mag.org/httpdocs(由 mediatemple 托管)。 我想要它,以便
好的旧正则表达式让我发疯。 我需要使用来自 mod_alias 的重定向(无法使用 mod_rewrite)将 Apache 2.4 中的所有流量从 HTTP 重定向到 HTTPS,“/bt/sub/
我有一个服务器,它的 httpd.conf 中已经有一些“RedirectMatch permanent”指令。 我对 mod_alias 不太熟悉,我只用过 mod_rewrite。 基本区别是什么
我有一个使用 mod_alias 的 Redirect 指令和 mod_rewrite 的 RewriteRule 的 apache 配置文件。 根据我的理解,mod_alias 按照文件从上到下的顺
我想使用 AliasMatch 为文件夹中的所有内容创建别名,除了两个(或更多)特定正则表达式模式。 例如,以下 AliasMatch 为“content”文件夹中的所有内容创建别名: AliasMa
我想使用 AliasMatch 为文件夹中的所有内容创建别名,除了两个(或更多)特定正则表达式模式。 例如,以下 AliasMatch 为“content”文件夹中的所有内容创建别名: AliasMa
我有一个网站,其中包含一组已放入 CMS 的旧 .html 和 .php 页面。 目前 .htaccess 文件中有大约 30 个 mod_alias 重定向,其形式如下: redirect 301
我是一名优秀的程序员,十分优秀!