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

标签 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(不包括查询字符串)? 例如:来自具有 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 中,您可以使用这样的代码来获取当前 URI 无需查询字符串:

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

关于PHP如何获取当前重写的URL(不包括查询字符串)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19872756/

相关文章:

php - 如何使用php和TCPDF在pdf中绘制虚线?

regex - Apache 多重重写规则

forms - html 表格 : checkboxes group for pretty url query

c# - Windows 8 手机 C# 请求桌面网站无法正常工作

php - Laravel 中的 session ID 有多独特?

php - 在 Wordpress 中使用纯 PhP/MySql 查询可以吗?

php - 如果您在 PHP 脚本实例运行时编辑它会发生什么情况?

.htaccess - mod重写htaccess问题

.htaccess - AWS ELB 在没有 index.php 的情况下将 HTTP 重定向到 HTTPS

用于验证绝对 URL 或相对 URL 的正则表达式