【问题标题】:.htaccess redirect all .html pages to process.php with get parameter.htaccess 使用 get 参数将所有 .html 页面重定向到 process.php
【发布时间】:2012-12-16 07:19:01
【问题描述】:

我想将所有具有 .html 扩展名的链接重定向到一个带有目录 + 页面名称作为 get 参数的 process.php 文件。我已经尝试过这些link1link2 解决方案,但它不起作用。

例如

http://localhost/Site/directory1/test1.html
http://localhost/Site/directory2/test2.html

重定向到 process.php 为

http://localhost/Site/process.php?directory1/test1.html
http://localhost/Site/process.php?directory2/test2.html

我试过这样。

RewriteEngine on
RewriteRule ^/(.*).html /process.php?page=$1

还有这个

RewriteCond %{REQUEST_URI} \.html$
RewriteRule ^(.*/)?([^/]*)\.php$ /process.php?page=$2 [R=301,L]

但它不起作用。

请查看并提出任何可行的方法。

【问题讨论】:

  • “它不起作用”不是问题。什么是 webroot 目录? .htaccess 文件的完整路径是什么?请启用重写日志并从日志中复制以显示 mod_rewrite 而不是的作用。此外,您使用了错误的术语。您的问题与重定向无关。

标签: mod-rewrite


【解决方案1】:

试试这个:

RewriteEngine on
RewriteRule ^([^/]*)/(.*\.html) /$1/process.php?page=$2 [R=301,L]

【讨论】:

  • @zerkms 这将导致:http://localhost/Site/process.php?page=directory1/test1.html,这不是 OP 想要的吗?
  • RewriteRule ^([^/]*)/(.*\.html) /index.php?page=$1/$2 [R=301,L] 这很好,谢谢。
【解决方案2】:

您的第二个示例与我相信您正在寻找的非常接近。当您应该查找 .html 时,您只需通过一个目录并查找 .php。试试这个。

RewriteEngine on
RewriteBase /
RewriteRule ^/(.*)/(.*)/(.*)\.html$ /Site/process.php?$2/$3 [R=301, L]

【讨论】:

    【解决方案3】:

    应该这样做(假设 .htaccess 文件位于 Site 目录中):

    RewriteEngine On
    RewriteCond %{REQUEST_URI} \.html$
    RewriteRule ^(.*?/)?(.*)$ /$1/process.php?page=$2 [R=301,L]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-05
      • 1970-01-01
      • 2022-06-30
      • 2016-04-22
      • 2016-07-20
      • 1970-01-01
      • 2018-02-04
      相关资源
      最近更新 更多