Solved

Does Mod Rewrite Change PHP_Self?

Posted on 2006-06-23
7
593 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

If your site has a few sections that need to be secure when data is transmitted between the server and local computer, such as a /order/ section for ordering or /customer/ which contains customer data, etc it would of course be recommended to secure…
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 is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

617 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