Solved

Does Mod Rewrite Change PHP_Self?

Posted on 2006-06-23
7
580 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
Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

If you've heard about htaccess and it sounds like it does what you want, but you're not sure how it works... well, you're in the right place. Read on. Some Basics #1. It's a file and its filename is .htaccess (yes, with a dot in the front). #…
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 …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

762 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

19 Experts available now in Live!

Get 1:1 Help Now