RewriteRule没有按预期工作 - htaccess

时间:2017-02-25 18:46:20

标签: php apache .htaccess mod-rewrite

我的.htaccess

RewriteEngine On

RewriteRule ^pg/(.*)$ /manage/product_groups/$1

我想将pg/fetchPG的每个网址重定向为第一和第二个网址段,如

http://website.com/pg/fetchPG?name=Name_here

http://website.com/manage/product_groups/fetchPG?name=Name_here

我还希望除此之外的任何网址都应重定向到http://website.net

我尝试通过在PHP中编写一些代码来执行此操作,但它执行302重定向。所以这不是我想要的。

mod_rewrite日志如下

[Sat Feb 25 17:18:03.920251 2017] [rewrite:trace3] [pid 8296] mod_rewrite.c(468): [client 39.32.81.36:26799] 39.32.81.36 - - [website.com/sid#7efcd2c4ec80][rid#7efcc588c0a0/initial] [perdir /var/www/html/] add path info postfix: /var/www/html/pg -> /var/www/html/pg/fetchPG
[Sat Feb 25 17:18:03.920306 2017] [rewrite:trace3] [pid 8296] mod_rewrite.c(468): [client 39.32.81.36:26799] 39.32.81.36 - - [website.com/sid#7efcd2c4ec80][rid#7efcc588c0a0/initial] [perdir /var/www/html/] strip per-dir prefix: /var/www/html/pg/fetchPG -> pg/fetchPG
[Sat Feb 25 17:18:03.920314 2017] [rewrite:trace3] [pid 8296] mod_rewrite.c(468): [client 39.32.81.36:26799] 39.32.81.36 - - [website.com/sid#7efcd2c4ec80][rid#7efcc588c0a0/initial] [perdir /var/www/html/] applying pattern '^pg/(.*)$' to uri 'pg/fetchPG'
[Sat Feb 25 17:18:03.920338 2017] [rewrite:trace2] [pid 8296] mod_rewrite.c(468): [client 39.32.81.36:26799] 39.32.81.36 - - [website.com/sid#7efcd2c4ec80][rid#7efcc588c0a0/initial] [perdir /var/www/html/] rewrite 'pg/fetchPG' -> 'manage/product_groups/fetchPG'
[Sat Feb 25 17:18:03.920349 2017] [rewrite:trace3] [pid 8296] mod_rewrite.c(468): [client 39.32.81.36:26799] 39.32.81.36 - - [website.com/sid#7efcd2c4ec80][rid#7efcc588c0a0/initial] [perdir /var/www/html/] add per-dir prefix: manage/product_groups/fetchPG -> /var/www/html/manage/product_groups/fetchPG
[Sat Feb 25 17:18:03.920363 2017] [rewrite:trace2] [pid 8296] mod_rewrite.c(468): [client 39.32.81.36:26799] 39.32.81.36 - - [website.com/sid#7efcd2c4ec80][rid#7efcc588c0a0/initial] [perdir /var/www/html/] trying to replace prefix /var/www/html/ with /
[Sat Feb 25 17:18:03.920375 2017] [rewrite:trace1] [pid 8296] mod_rewrite.c(468): [client 39.32.81.36:26799] 39.32.81.36 - - [website.com/sid#7efcd2c4ec80][rid#7efcc588c0a0/initial] [perdir /var/www/html/] internal redirect with /manage/product_groups/fetchPG [INTERNAL REDIRECT]
[Sat Feb 25 17:18:03.920959 2017] [rewrite:trace3] [pid 8296] mod_rewrite.c(468): [client 39.32.81.36:26799] 39.32.81.36 - - [website.com/sid#7efcd2c4ec80][rid#7efcc5886330/initial/redir#1] [perdir /var/www/html/manage/] add path info postfix: /var/www/html/manage/product_groups -> /var/www/html/manage/product_groups/fetchPG
[Sat Feb 25 17:18:03.920976 2017] [rewrite:trace3] [pid 8296] mod_rewrite.c(468): [client 39.32.81.36:26799] 39.32.81.36 - - [website.com/sid#7efcd2c4ec80][rid#7efcc5886330/initial/redir#1] [perdir /var/www/html/manage/] strip per-dir prefix: /var/www/html/manage/product_groups/fetchPG -> product_groups/fetchPG
[Sat Feb 25 17:18:03.920984 2017] [rewrite:trace3] [pid 8296] mod_rewrite.c(468): [client 39.32.81.36:26799] 39.32.81.36 - - [website.com/sid#7efcd2c4ec80][rid#7efcc5886330/initial/redir#1] [perdir /var/www/html/manage/] applying pattern '(^|/)\\.' to uri 'product_groups/fetchPG'
[Sat Feb 25 17:18:03.920992 2017] [rewrite:trace3] [pid 8296] mod_rewrite.c(468): [client 39.32.81.36:26799] 39.32.81.36 - - [website.com/sid#7efcd2c4ec80][rid#7efcc5886330/initial/redir#1] [perdir /var/www/html/manage/] add path info postfix: /var/www/html/manage/product_groups -> /var/www/html/manage/product_groups/fetchPG
[Sat Feb 25 17:18:03.920999 2017] [rewrite:trace3] [pid 8296] mod_rewrite.c(468): [client 39.32.81.36:26799] 39.32.81.36 - - [website.com/sid#7efcd2c4ec80][rid#7efcc5886330/initial/redir#1] [perdir /var/www/html/manage/] strip per-dir prefix: /var/www/html/manage/product_groups/fetchPG -> product_groups/fetchPG
[Sat Feb 25 17:18:03.921006 2017] [rewrite:trace3] [pid 8296] mod_rewrite.c(468): [client 39.32.81.36:26799] 39.32.81.36 - - [website.com/sid#7efcd2c4ec80][rid#7efcc5886330/initial/redir#1] [perdir /var/www/html/manage/] applying pattern '^(.*)$' to uri 'product_groups/fetchPG'
[Sat Feb 25 17:18:03.921020 2017] [rewrite:trace3] [pid 8296] mod_rewrite.c(468): [client 39.32.81.36:26799] 39.32.81.36 - - [website.com/sid#7efcd2c4ec80][rid#7efcc5886330/initial/redir#1] [perdir /var/www/html/manage/] add path info postfix: /var/www/html/manage/product_groups -> /var/www/html/manage/product_groups/fetchPG
[Sat Feb 25 17:18:03.921027 2017] [rewrite:trace3] [pid 8296] mod_rewrite.c(468): [client 39.32.81.36:26799] 39.32.81.36 - - [website.com/sid#7efcd2c4ec80][rid#7efcc5886330/initial/redir#1] [perdir /var/www/html/manage/] strip per-dir prefix: /var/www/html/manage/product_groups/fetchPG -> product_groups/fetchPG
[Sat Feb 25 17:18:03.921041 2017] [rewrite:trace3] [pid 8296] mod_rewrite.c(468): [client 39.32.81.36:26799] 39.32.81.36 - - [website.com/sid#7efcd2c4ec80][rid#7efcc5886330/initial/redir#1] [perdir /var/www/html/manage/] applying pattern '^' to uri 'product_groups/fetchPG'
[Sat Feb 25 17:18:03.921055 2017] [rewrite:trace3] [pid 8296] mod_rewrite.c(468): [client 39.32.81.36:26799] 39.32.81.36 - - [website.com/sid#7efcd2c4ec80][rid#7efcc5886330/initial/redir#1] [perdir /var/www/html/manage/] add path info postfix: /var/www/html/manage/product_groups -> /var/www/html/manage/product_groups/fetchPG
[Sat Feb 25 17:18:03.921063 2017] [rewrite:trace3] [pid 8296] mod_rewrite.c(468): [client 39.32.81.36:26799] 39.32.81.36 - - [website.com/sid#7efcd2c4ec80][rid#7efcc5886330/initial/redir#1] [perdir /var/www/html/manage/] strip per-dir prefix: /var/www/html/manage/product_groups/fetchPG -> product_groups/fetchPG
[Sat Feb 25 17:18:03.921069 2017] [rewrite:trace3] [pid 8296] mod_rewrite.c(468): [client 39.32.81.36:26799] 39.32.81.36 - - [website.com/sid#7efcd2c4ec80][rid#7efcc5886330/initial/redir#1] [perdir /var/www/html/manage/] applying pattern '^(.*)$' to uri 'product_groups/fetchPG'
[Sat Feb 25 17:18:03.921085 2017] [rewrite:trace2] [pid 8296] mod_rewrite.c(468): [client 39.32.81.36:26799] 39.32.81.36 - - [website.com/sid#7efcd2c4ec80][rid#7efcc5886330/initial/redir#1] [perdir /var/www/html/manage/] rewrite 'product_groups/fetchPG' -> 'index.php?/product_groups/fetchPG'
[Sat Feb 25 17:18:03.921093 2017] [rewrite:trace3] [pid 8296] mod_rewrite.c(468): [client 39.32.81.36:26799] 39.32.81.36 - - [website.com/sid#7efcd2c4ec80][rid#7efcc5886330/initial/redir#1] split uri=index.php?/product_groups/fetchPG -> uri=index.php, args=/product_groups/fetchPG
[Sat Feb 25 17:18:03.921105 2017] [rewrite:trace3] [pid 8296] mod_rewrite.c(468): [client 39.32.81.36:26799] 39.32.81.36 - - [website.com/sid#7efcd2c4ec80][rid#7efcc5886330/initial/redir#1] [perdir /var/www/html/manage/] add per-dir prefix: index.php -> /var/www/html/manage/index.php
[Sat Feb 25 17:18:03.921114 2017] [rewrite:trace2] [pid 8296] mod_rewrite.c(468): [client 39.32.81.36:26799] 39.32.81.36 - - [website.com/sid#7efcd2c4ec80][rid#7efcc5886330/initial/redir#1] [perdir /var/www/html/manage/] strip document_root prefix: /var/www/html/manage/index.php -> /manage/index.php
[Sat Feb 25 17:18:03.921120 2017] [rewrite:trace1] [pid 8296] mod_rewrite.c(468): [client 39.32.81.36:26799] 39.32.81.36 - - [website.com/sid#7efcd2c4ec80][rid#7efcc5886330/initial/redir#1] [perdir /var/www/html/manage/] internal redirect with /manage/index.php [INTERNAL REDIRECT]
[Sat Feb 25 17:18:03.921192 2017] [rewrite:trace3] [pid 8296] mod_rewrite.c(468): [client 39.32.81.36:26799] 39.32.81.36 - - [website.com/sid#7efcd2c4ec80][rid#7efcc58794c0/initial/redir#2] [perdir /var/www/html/manage/] strip per-dir prefix: /var/www/html/manage/index.php -> index.php
[Sat Feb 25 17:18:03.921202 2017] [rewrite:trace3] [pid 8296] mod_rewrite.c(468): [client 39.32.81.36:26799] 39.32.81.36 - - [website.com/sid#7efcd2c4ec80][rid#7efcc58794c0/initial/redir#2] [perdir /var/www/html/manage/] applying pattern '(^|/)\\.' to uri 'index.php'
[Sat Feb 25 17:18:03.921211 2017] [rewrite:trace3] [pid 8296] mod_rewrite.c(468): [client 39.32.81.36:26799] 39.32.81.36 - - [website.com/sid#7efcd2c4ec80][rid#7efcc58794c0/initial/redir#2] [perdir /var/www/html/manage/] strip per-dir prefix: /var/www/html/manage/index.php -> index.php
[Sat Feb 25 17:18:03.921217 2017] [rewrite:trace3] [pid 8296] mod_rewrite.c(468): [client 39.32.81.36:26799] 39.32.81.36 - - [website.com/sid#7efcd2c4ec80][rid#7efcc58794c0/initial/redir#2] [perdir /var/www/html/manage/] applying pattern '^(.*)$' to uri 'index.php'
[Sat Feb 25 17:18:03.921226 2017] [rewrite:trace3] [pid 8296] mod_rewrite.c(468): [client 39.32.81.36:26799] 39.32.81.36 - - [website.com/sid#7efcd2c4ec80][rid#7efcc58794c0/initial/redir#2] [perdir /var/www/html/manage/] strip per-dir prefix: /var/www/html/manage/index.php -> index.php
[Sat Feb 25 17:18:03.921240 2017] [rewrite:trace3] [pid 8296] mod_rewrite.c(468): [client 39.32.81.36:26799] 39.32.81.36 - - [website.com/sid#7efcd2c4ec80][rid#7efcc58794c0/initial/redir#2] [perdir /var/www/html/manage/] applying pattern '^' to uri 'index.php'
[Sat Feb 25 17:18:03.921255 2017] [rewrite:trace3] [pid 8296] mod_rewrite.c(468): [client 39.32.81.36:26799] 39.32.81.36 - - [website.com/sid#7efcd2c4ec80][rid#7efcc58794c0/initial/redir#2] [perdir /var/www/html/manage/] strip per-dir prefix: /var/www/html/manage/index.php -> index.php
[Sat Feb 25 17:18:03.921262 2017] [rewrite:trace3] [pid 8296] mod_rewrite.c(468): [client 39.32.81.36:26799] 39.32.81.36 - - [website.com/sid#7efcd2c4ec80][rid#7efcc58794c0/initial/redir#2] [perdir /var/www/html/manage/] applying pattern '^(.*)$' to uri 'index.php'
[Sat Feb 25 17:18:03.921274 2017] [rewrite:trace1] [pid 8296] mod_rewrite.c(468): [client 39.32.81.36:26799] 39.32.81.36 - - [website.com/sid#7efcd2c4ec80][rid#7efcc58794c0/initial/redir#2] [perdir /var/www/html/manage/] pass through /var/www/html/manage/index.php

1 个答案:

答案 0 :(得分:2)

编辑/记录:要查看服务器配置文件中发生了什么,enable logging for mod_rewrite。如果您需要帮助,请告诉我。在文件(可能是httpd.conf)中,找到LogLevel指令并将rewrite:trace2添加到该行。请记住,您必须重新启动apache才能使日志记录生效。

首先,我怀疑您的解决方案是使用RewriteBase directive。没有它,很难知道替换前加上了什么前缀。在像.htaccess这样的文件设置中,替换的前导斜杠可能会导致问题。

其次,使用the QSA flag附加原始查询字符串:

RewriteEngine On
RewriteBase /
RewriteRule ^pg/(.*)$ manage/product_groups/$1 [QSA]