SCC
SCC

Reputation: 509

PHP how to get current rewrited URL excluding querystring?

I am using the following URL rewriting in .htaccess.

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]

Then How to get current rewrited URL excluding querystring? For eg: From the page having URL: /22/somestring/enquiry?value=5&page=9&item=coffee I expect: /22/somestring/enquiry

I tried with $_SERVER['REQUEST_URI'] but it return /22/somestring/enquiry?value=5&page=9&item=coffee And also $_SERVER['REDIRECT_URL'] give me what I expected, but it generates an Notice: Undefined index: REDIRECT_URL in some pages. And also it doesn't work in some servers.

Upvotes: 2

Views: 5326

Answers (2)

anubhava
anubhava

Reputation: 785156

In you PHP you can use code like this to get current URI without query string:

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

Upvotes: 2

helion3
helion3

Reputation: 37381

I don't believe PHP has a superglobal for the URL without a query string, but you can manually strip the query string out:

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

Upvotes: 0

Related Questions