Jump to content

Request_uri path_info query_string


Rebelrebellious

Recommended Posts

Hello. This is my first visit to PHP Freaks. Hopefully someday I will also be a PHP Freak. Today I have a multipart question.

 

1. I am trying to implement a script to translate all URI requests into lowercase requests. Since I will be making all of my site URIs lowercase this will hopefully make my site addresses case insensitive. Is this a good idea?

 

2. I found this script, which I understand to go into two files. Does this script look okay?

 

script 1:

<?php
/*
Plugin Name: unFocus.Insensitivity
Plugin URI: http://www.unfocus.com/projects/
Description: A plugin to make permalinks case insensitive.
Version: 1.0a
Author: Kevin Newman
Author URI: http://www.unfocus.com/projects/
*/
function unFocus_insensitivity() {
if (preg_match('/[A-Z]/', $_SERVER['REQUEST_URI'])) {
	$_SERVER['REQUEST_URI'] = strtolower($_SERVER['REQUEST_URI']);
	$_SERVER['PATH_INFO']   = strtolower($_SERVER['PATH_INFO']);
}
}
add_action('init', 'unFocus_insensitivity');
?>

 

script 2:

<?php
$_SERVER['REQUEST_URI'] = substr(
$_SERVER['QUERY_STRING'],
strpos(
	$_SERVER['QUERY_STRING'],
	':80'
) +3
);
$_SERVER['PATH_INFO'] = $_SERVER['REQUEST_URI'];

include('index.php');
?>

 

 

3. Can you determine which two files the script should be placed in?

 

4. I have been trying to figure out the script myself. The first script seems like it should actually replace the URI with a lowercase version of the URI. Why does it also replace PATH_INFO with a lowercase version?

 

5. The second script seems to be replacing REQUEST_URI with the contents of QUERY_STRING that come after the string :80 and then assigning the same data to PATH_INFO. Why?

 

6. To investigate these postulations I wrote the following script, but I only get the following output. Could you explain this?

 

my script:

<html>
<head>
  <title>PHP Test</title>
</head>
<body>
<p> 1 
<?php echo $_SERVER['REQUEST_URI']; ?>
</p>
<?php
$_SERVER['REQUEST_URI'] = substr(
$_SERVER['QUERY_STRING'],
strpos(
	$_SERVER['QUERY_STRING'],
	':80'
) +3
); ?>
<p> 2 
<?php echo $_SERVER['REQUEST_URI']; ?>
</p>
<p> 3 
<?php echo $_SERVER['PATH_INFO']; ?>
</p>
<?php $_SERVER['PATH_INFO'] = $_SERVER['REQUEST_URI']; ?>
<p> 4 
<?php echo $_SERVER['PATH_INFO']; ?>
</p>
<?php include('index.php'); ?>
</body>
</html>

 

the output:

1 /querytest.php

2 

3 

4 
echo '

This is index.php
';

 

Thank you.

Link to comment
Share on other sites

Well, before we get into these scripts, I have a question. Are you looking for something where the following URL:

http://www.hostname.com/mypage.php

can be accessed by any of the following URLs:

http://www.hostname.com/mypage.php
http://www.hostname.com/myPage.php
http://www.hostname.com/MyPage.php
http://www.hostname.com/MYPAGE.php

etc?

Link to comment
Share on other sites

I always hate people who ignore the OP's question and offer a different solution, but I have to be "that guy".  Instead of adding of this complexity with changing global variables for the sake of case-insensitivity, why not just use the appropriate error handler to do this for you?  On your 404 page, you can try and see if the the lowercase version of the page exists.  If it does, use a 301 redirect to send the user to that page.  This has an added SEO benefit, and it separates the logic.

 

If I'm missing the reasoning for this plugin, just ignore me.

Link to comment
Share on other sites

In that case, you will have to begin with something Apache based. Apache needs to call a PHP page for PHP code to be run. So, if those files don't exist, Apache won't know what PHP to call. I do recommend looking into BrandonK's topic, as it is a good idea. You should be able to define a custom 404 page using an htaccess file. Either way, this should be moved over to the Apache Help forum.

 

 

On a side note, I don't believe Window's based servers are case sensitive, but I may be wrong.

Link to comment
Share on other sites

This will be my first working php script, but I am feeling pretty good about it since every script claiming to do this was broken in some manner. I could not verify the files existence without bogging down the server for about 60 seconds and/or prompting a php error since the files are dynamically generated by wordpress. As a result I just check if caps are used and if they are I try the lowercase version before going to 404. Thank you for pointing me in the right direction.

 

404.php:

<?php

#check if URL contains any capital letters
if (preg_match('/[A-Z]/', $_SERVER['REQUEST_URI']))
{
#convert the URL to lower case
$lowercase_file_url = strtolower("http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
    /* Redirect browser */
header("HTTP/1.1 303 See Other");
header("Location: $lowercase_file_url");
    /* Make sure that code below does not get executed when we redirect. */
exit();
}
# if it doesn't exist, show a 404 error
else
{
get_header();
}
?>

<div id="content" class="narrowcolumn">

	<h2 class="center">Error 404 - Not Found</h2>

</div>

<?php get_sidebar(); ?>

<?php get_footer(); ?>

 

If you see anything that I should fix now, please suggest it.

 

Would anyone be kind enough to help me debug the scripts that I presented in the initial question so that I can learn more about how php works?

 

One specific question I have is why the .'s are used like they are in my final script. It didn't work without them, but I could not do any effective web searches for . so it is a magic character for me at this point in time. Thank again.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.