0

I'm trying to get the URL in the address bar using PHP, but $_SERVER['REQUEST_URI'] returns that path to the actual file, not the URL I'm created using .htaccess. For example, if my file is at

example.com/files/example.php

But I've changes the url to:

example.com/example

I want to get the prettier URL, using PHP, not the actual path. $_SERVER['REQUEST_URI'] is returning the actual path.

user2250471
  • 1,002
  • 3
  • 10
  • 23
  • @this.lau_ Not a duplicate. That answer will get the url after the redirect – Basic Jul 12 '14 at 22:37
  • Surely if you know how you're rewriting the url in the htaccess file you can easily use the same logic in php on $_SERVER['REQUEST_URI'] to make your rewritten url? http://stackoverflow.com/questions/6588661/php-how-can-i-get-the-url-that-has-been-rewritten-with-mod-rewrite answer may help you. – Roy Jul 12 '14 at 22:38
  • 1
    I dont get it `$_SERVER['REQUEST_URI']` will return what is in the address bar, unless your doing something wacky with your rewrites, show them if you can. – Lawrence Cherone Jul 12 '14 at 22:40
  • @Basic, you're correct, I guess the duplicate should the question/answer mentioned by user1. – laurent Jul 12 '14 at 22:40

2 Answers2

1

If your server redirects the browser to the new URL, you could get the old URL from $_SERVER['HTTP_REFERER'].

Daniel
  • 3,541
  • 3
  • 33
  • 46
0
$prettyUrl= str_replace(array('files/', '.php'), ' ', $_SERVER['PHP_SELF']);

Obviously different logic depending on what page and its corresponding htaccess rewrite.

Though of course as you're referring to $_SERVER['PHP_SELF'] one would assume you're talking about a redirect? and if so that url should rewritten already as it's sent from the browser.

Roy
  • 3,027
  • 4
  • 29
  • 43