【问题标题】:htaccess rewrite after redirecthtaccess 重定向后重写
【发布时间】:2016-05-01 10:51:53
【问题描述】:

我有一个网站,它使用 htaccess 将所有页面重写到索引页面,并使用哈希值来提供内容。文件长这样……

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^?]*)$ /index.php?urlpath=$1 [NC,L,QSA]

我现在正在移动网站的一些页面,但是如果我添加重定向,例如......

Redirect 301 /blog /new_location/blog/

我遇到的问题是生成的 url 看起来像

https://mydomain/new_location/blog/urlpath=blog.php

谁能建议我让页面重定向到 mydomain/new_location/blog/ 然后在新 url 上运行重写的方法。

非常感谢

【问题讨论】:

    标签: apache .htaccess redirect mod-rewrite


    【解决方案1】:

    RewriteRuleRedirect 来自不同的 Apache 模块,因此在处理过程中运行在不同的时间,而不是按照它们在配置中出现的顺序。你最好坚持使用一个或另一个模块,通过使用[R] 标志到RewriteRule

    RewriteRule /blog(.*) /new_location/blog$1 [R=301]
    

    【讨论】:

    • 我已将 Redirect 更改为 RewriteRule,但由于新 url (/new_location/blog) 未传递到索引页面,因此它没有像希望的那样工作,因此它正在尝试查找使用原始 url (/blog) 的页面内容找不到任何内容。
    【解决方案2】:

    好的,我设法使用重定向和重写的组合来完成这项工作......

    Redirect 301 /blog /new_location/blog
    
    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !/blog
    RewriteRule ^([^?]*)$ /index.php?urlpath=$1 [NC,L,QSA]
    

    也许不是最简洁的解决方案,但它有效!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-26
      • 2021-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多