PHP如何获取当前重写的URL不包括查询字符串?

时间:2013-11-09 05:38:10

标签: php .htaccess url url-rewriting

我在.htaccess中使用以下URL重写。

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteRule ^([0-9]+)/([a-zA-Z0-9\&_,-]+)$ business/business_home.php?business_id=$1 [L,QSA]
RewriteRule ^([0-9]+)/([a-zA-Z0-9\&_,-]+)/(about)$ business/business_about.php?business_id=$1 [L,QSA]
RewriteRule ^([0-9]+)/([a-zA-Z0-9\&_,-]+)/(products)$ business/business_products.php?business_id=$1 [L,QSA]
RewriteRule ^([0-9]+)/([a-zA-Z0-9\&_,-]+)/(enquiry)$ business/business_enquiry.php?business_id=$1 [L,QSA]
RewriteRule ^([0-9]+)/([a-zA-Z0-9\&_,-]+)/(contact)$ business/business_contact.php?business_id=$1 [L,QSA]

## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php[\s\?] [NC]
RewriteRule ^ - [R=404,L]

# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ /$1.php [L]

然后如何获取当前重写的URL,不包括查询字符串? 例如:从包含网址的网页/22/somestring/enquiry?value=5&page=9&item=coffee 我希望:/22/somestring/enquiry

我尝试使用$_SERVER['REQUEST_URI'],但它返回/22/somestring/enquiry?value=5&page=9&item=coffee 并且$_SERVER['REDIRECT_URL']也给了我预期的结果,但它在某些页面中生成Notice: Undefined index: REDIRECT_URL。还有一些服务器it doesn't work

2 个答案:

答案 0 :(得分:2)

在PHP中,您可以使用这样的代码来获取当前URI 而不使用查询字符串

$thisuri = preg_replace('/\?.*$/', '', $_SERVER["REQUEST_URI"]);

答案 1 :(得分:0)

我不认为PHP没有查询字符串的URL有超全局,但您可以手动删除查询字符串:

protected function stripQuery($str){
  return preg_replace('/\?(.*)/', '', $str);
}