Solved

Does Mod Rewrite Change PHP_Self?

Posted on 2006-06-23
7
586 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
Portable, direct connect server access

The ATEN CV211 connects a laptop directly to any server allowing you instant access to perform data maintenance and local operations, for quick troubleshooting, updating, service and repair.

 
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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
RewrteRule using htacess 10 76
PHP Error Settings 12 84
Missing Apache mod_DBD packages in Centos 7 2 87
How code a 301 redirect for folder files -> 1 file 2 58
As Wikipedia explains 'robots.txt' as -- the robot exclusion standard, also known as the Robots Exclusion Protocol or robots.txt protocol, is a convention to prevent cooperating web spiders and other web robots from accessing all or part of a websit…
It is possible to boost certain documents at query time in Solr. Query time boosting can be a powerful resource for finding the most relevant and "best" content. Of course the more information you index, the more fields you will be able to use for y…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

860 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