Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

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

$thisuri = preg_replace('/\?.*$/', '', $_SERVER["REQUEST_URI"]);
share|improve this answer
 
if ( isset($_SERVER["REDIRECT_URL"]) ) , $_SERVER["REDIRECT_URL"] itself generate current URI without query string. Then why use preg_replace –  user2826057 Nov 9 '13 at 6:05
 
You can skip preg_replace in first if. Let me edit. –  anubhava Nov 9 '13 at 6:16
 
Try edited code now. –  anubhava Nov 9 '13 at 6:16
1  
Parse error: syntax error, unexpected '/', expecting ')' in –  user2826057 Nov 9 '13 at 6:20
 
Oh sorry forgot quotes around regex. Try it now. –  anubhava Nov 9 '13 at 6:33
show 10 more comments

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);
}
share|improve this answer
1  
See the question. $_SERVER['REDIRECT_URL'] give me what I expected. Isn't it a superglobal –  user2826057 Nov 9 '13 at 6:29
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.