【问题标题】:.htaccess 301 and PHP query string.htaccess 301 和 PHP 查询字符串
【发布时间】:2016-06-19 19:36:51
【问题描述】:

我想知道 SO 社区的精彩世界是否可以帮助我解决这个问题。

我想在.htaccess 文件中重定向/重写以下 URL。

1。重定向

我正在尝试 301 重定向此 URL:

http://example.com/staff-view.php?i=ACCOUNT_ID_EXAMPLE

http://example.com/staff-view/ACCOUNT_ID_EXAMPLE/

我尝试了以下方法:

RewriteEngine On

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+staff-view\.php\?i=([^\s]+) [NC]
RewriteRule ^ /staff-view/%1/? [R=301,L]

但是,当导航到来自 URL 时,它不会重定向。

2。 PHP 查询字符串

我正在使用下面的重写:

RewriteRule ^staff-view/([^/]+)/?$ /staff-view.php?i=$1 [L,QSA,NC]

通过 URL 访问

http://example.com/staff-view/ACCOUNT_ID_EXAMPLE/

它正确地将我引导到正确的页面,但是当尝试通过以下方法访问 ACCOUNT_ID_EXAMPLE 时:

<?
var_dump($_REQUEST);
var_dump($_GET);
?>

它们都是空的:

array(0) { } array(0) { }

非常感谢您的帮助,如果您需要更多信息,请告诉我。


更新 1

更新.htaccess文件:

RewriteEngine On

RewriteRule ^staff/([^/]+)/?$ /staff-view.php?i=$1 [L,QSA,NC]

RewriteBase "/"

RewriteCond %{REQUEST_URI}  ^staff-view\.php$
RewriteCond %{QUERY_STRING} ^i=([A-Z0-9_]*)$
RewriteRule ^(.*)$  ^staff/%1/

我已尝试从以下位置访问文件:

http://example.com/path/to/site/staff/ACCOUNT_ID

它不起作用。但是,如果我从以下位置访问文件:

http://example2.com/staff/ACCOUNT_ID

它工作。

但是如果我转到http://example2.com/staff-view.php?i=ACCOUNT_ID,它不会重定向到http://example2.com/staff/ACCOUNT_ID - 这不是世界末日,但我想修复它,但优先考虑深层目录问题:)

【问题讨论】:

  • 您的RewriteRule 都有L 标志,这意味着停止处理。此外,它们看起来是循环的(一个重定向到另一个,另一个重定向到另一个)。这可能是问题的一部分吗?如果您将 staff-view.php 重命名为其他名称并将路径 staff-view/id/ 重定向到它,事情会更好吗?然后当我请求staff-view.php?id=1 时,我将被定向到staff-view/1/(带有R=301),然后到new-file.php?id=1(静默)?

标签: php apache .htaccess mod-rewrite query-string


【解决方案1】:

试试这个:

RewriteEngine On
RewriteRule ^staff-view/([^/]*)$ /staff-view.php?i=$1 [L]

【讨论】:

  • 感谢解答,我试过了,还是没有返回ACCOUNT_ID_EXAMPLE的查询字符串
【解决方案2】:

尝试解析%{THE_REQUEST}(例如,“GET /staff-view.php?i=account_id HTTP/1.1”)来提取路径和查询字符串可以工作,但它不必要地复杂。我认为使用两个利用服务器变量 %{REQUEST_URI}%{QUERY_STRING} 的重写条件要简单得多,因为它们预先填充了您感兴趣的信息。

尝试以下方法:

RewriteEngine On

#If you don't have this set in your htaccess, 
#  Apache may prepend the final path with your on disk dir structure
RewriteBase "/"

#Rewrite if the /staff-view.php page is requested
RewriteCond %{REQUEST_URI}  ^/staff-view\.php$

#Rewrite if the query string contains i parameter anywhere. Assumes
#  ID can be only digits; include all allowed chars. eg: [a-zA-Z0-9_]    
#Don't forget the '&' before {{QUERY_STRING}} otherwise the match
#  will fail if i is the first parameter
RewriteCond &%{QUERY_STRING} &i=([0-9]+)

#Rewrite the account ID as part of the path. Append the query string
#  in order to preserve other query parameters (eg: if user asked for
#  /staff-view.php?i=123&x=boo, you want to preserve x=boo.  a 301
#  redirect tells the browser to go to the new path and to remember it
#This will stop processing and cause the browser to make a new request
RewriteRule ^(.*)$  /staff-view/%1/ [QSA,R=301,L]

#Finally, we want to silently forward any request matching the last
#  redirect target to the actual file that will serve the request. 
#  The account ID should be of the same format as above: ([0-9]+).  The
#  [L] flag tells the server to stop looking for new instructions
RewriteRule ^staff-view/([0-9]+)/$ /final.php?i=$1 [QSA,L]

逻辑很容易理解:如果请求的路径是/staff-view.php,并且如果查询字符串包含i参数,则告诉用户的浏览器转到/staff-view/ID,保留其他查询参数。最后,当浏览器请求这个新路径时,静默(不告诉浏览器)将该请求连同 ID 和其他查询参数一起转发到 final.php

【讨论】:

  • 找到500错误的RCA后,仍然没有重定向或输出查询字符串:(
  • 在您的 Q 下提出了一个建议。您可以在将最终目的地重命名为 staff-view.php 以外的其他名称后进行测试吗?
  • 我已经通过“尝试”将/staff/ID 定向到staff-view.php 来尝试您的建议。还是不做。
  • 我不是有意更改/staff-view/ID 路径;我的意思是重命名staff-view.php,调整第2点下的规则以指向新名称,并仍然尝试访问旧网址(staff-view.php?id=1staff-view/ID)上的内容
  • 更正,我想我刚刚发现问题 #2 的问题。如果我尝试从这样的目录访问文件:/path/to/site/dir/staff/ACCOUNT_ID,PHP 找不到变量并且它不起作用。如果我从http://example.com/staff/ACCOUNT_ID 访问它,它可以工作,PHP 会返回详细信息。
猜你喜欢
  • 2020-03-18
  • 1970-01-01
  • 2017-04-13
  • 2011-04-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-02
相关资源
最近更新 更多