【发布时间】:2013-11-21 06:31:03
【问题描述】:
我在 .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,不包括查询字符串?
例如:来自具有 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。
【问题讨论】:
标签: php .htaccess url url-rewriting