Solved

Does Mod Rewrite Change PHP_Self?

Posted on 2006-06-23
7
582 Views
Last Modified: 2012-06-27
If I use mod rewrite to fake .html endings on my php files (i.e. the URL discussion/viewpost=15.html calls the script otherwise located at discussion.php?viewpost=15), and then use $_SERVER['PHP_SELF'] to determine the current URL, will it give me the URL that was typed into the browser window (or clicked on), or the one that is actually being called on the server? In otherwords, will it return discussion/viewpost=15.html or discussion.php?viewpost=15 ?

Normally I would just go and test something like this, but I haven't figured out how to use mod rewrite yet, and I want to know how it is going to affect my scripts once I implement it.
0
Comment
Question by:jfredrickson
  • 5
  • 2
7 Comments
 
LVL 21

Expert Comment

by:Julian Matz
ID: 16976977
I think PHP_SELF will return the real URL.
I'll test this now to confirm...
0
 
LVL 21

Accepted Solution

by:
Julian Matz earned 500 total points
ID: 16976983
Yes, it returns the real URL.

For example if
index.html --> redirects to --> index.php
and /index.html is entered in the browser, PHP_SELF will return /index.php.
0
 
LVL 4

Author Comment

by:jfredrickson
ID: 16977010
Awesome. And if I understand correctly, Mod Rewrite doesn't actually redirect in the sense that javascript does by actually forwarding the browser, but simply display a different page right? So in reality, the user types in index.html, the server displays index.php, PHP_SELF returns /index.php, and the URL in the browser bar remains only displaying index.html as if it is just another HTML page. Is that right?
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 21

Expert Comment

by:Julian Matz
ID: 16977027
Yes, that's correct :)

And index.html doesn't actually need to exist for it to work.
But you can use mod_rewrite to actually redirect the page if you wish. The example below would do a valid permanent redirect and send a 301 status code to the browser (or search engine):

RewriteEngine On
RewriteRule ^viewpost([0-9]*)\.html$ /index.php?viewpost=$1 [R=301,L]

The "R" flag will cause the page to redirect and the "=301" will cause a permanent redirect (the one you should always use to keep search engines happy).

If you just want to rewrite the URL without the redirect, you can simply leave out the "R" flag...
0
 
LVL 21

Expert Comment

by:Julian Matz
ID: 16977044
I just wanted to add that you don't need the 301 redirect when using mod_rewrite to keep search engines "happy" because a rewrite will be transparent to the search engine anyway... I just mentioned it before in comparison to javascript or normal php redirect...

To redirect a page using php, you could do:

<?php
header ('HTTP/1.1 301 Moved Permanently');
header ('Location: http://www.example.com/newpage.php');
header ('Connection: close');
exit;
?>

The above will work on search engines and also on browsers that have javascript disabled...


I've rambled on a little now :) If any of this doesn't make sense, please let me know...
0
 
LVL 4

Author Comment

by:jfredrickson
ID: 16977048
Wow, I am getting more than I had asked for here. I am going to accept your first answer because that really answered my question, and rather than getting more answers for other questions on the same 500 points, I opened up another question so that you can help me with the Rewrite stuff.

I am not 100% sure that this is the correct protocol for this site, but it is my understanding that we are only supposed to ask 1 question at a time so that experts recieve points for every question they answer.

Anyway, here is the link for the new question. If you reply there I can give you points for your help with the Rewrite stuff too.

http://www.experts-exchange.com/Web/Web_Servers/Apache/Q_21898173.html

Thanks for the awesome help.
0
 
LVL 21

Expert Comment

by:Julian Matz
ID: 16977086
Thanks for the A, jfredrickson! Glad I was able to help...

Yes, that is the correct protocol... I wouldn't have minded answering your question here at all, but appreciate the points also :)

-Julian.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
local dns 2 136
How can I write .htaccess code to redirect 301 pages? 1 111
Configure a Local PHP Interpreter for Phpstorm 2 192
linux apache localhost dev laptop 5 48
Over the last year I have answered a couple of basic URL rewriting questions several times so I thought I might as well have a stab at: explaining the basics, providing a few useful links and consolidating some of the most common queries into a sing…
In Solr 4.0 it is possible to atomically (or partially) update individual fields in a document. This article will show the operations possible for atomic updating as well as setting up your Solr instance to be able to perform the actions. One major …
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now