remove variables from url?

Discuss practical ways rearrange URLs using mod_rewrite.

remove variables from url?

Postby captain_caveman » Sat Jan 15, 2005 12:51 pm

I want to have a page can have variable sent to itself via form, but doesn't show any variables in the location bar of the browser.

Here is the php code in (path/file.php)
Code: Select all
<?php
ob_start();
header("Location: http://" . $_SERVER['HTTP_HOST']
. dirname($_SERVER['PHP_SELF'])
. "file/" . ($entry));
ob_end_flush();
?>

This will send the $entry variable back to itself (path/file/$entry)

Here is the .htaccess file in (path/.htaccess)
Code: Select all
RewriteEngine on
Rewrite Base /
RewriteRule ^file/(.*)$ file.php?entry=$1 [L]

This should? take file/var format and interpret as file.php?entry=var.

The theory dosn't work.
I don't know why.
Can this be fixed or is there another way to do it?

another senario:
if I have another file called files.php that has the above php code in it,
and I have a file called file.php without the above php code,
then it will work once. (pass from files to file)
Just doesnt seem to want to work when passed to itself? (file to file)

signed,
Stumped but not felled.
captain_caveman
 
Posts: 7
Joined: Sat Jan 15, 2005 12:16 pm

Postby Caterham » Sun Jan 16, 2005 5:19 am

You are writing path/.htaccess but using a RewriteBase /???. You don't need a RewriteBase unless you are using an alias with mod_alias.

either use
Code: Select all
RewriteEngine on
RewriteRule ^file/(.*)$ file.php?entry=$1 [L]


or
Code: Select all
RewriteEngine on
RewriteRule ^file/(.*)$ /path/file.php?entry=$1 [L]


or with the redundant Rw-Base

Code: Select all
RewriteEngine on
RewriteBase /path
RewriteRule ^file/(.*)$ file.php?entry=$1 [L]

or
Code: Select all
RewriteEngine on
RewriteBase /path
RewriteRule ^file/(.*)$ /path/file.php?entry=$1 [L]


The theory dosn't work.
I don't know why.
If the rules above doesn't work: Whar kind of error do you get?

Just doesnt seem to want to work when passed to itself? (file to file)
It would work, if the pattern of the RewriteRule matches. But make sure, that you don't create a loop with the location header.

Bob
Caterham
 
Posts: 690
Joined: Fri Dec 10, 2004 1:30 pm

Postby captain_caveman » Sun Jan 16, 2005 5:36 pm

All righty, I was hoping that you would reply.
Looks like you are one of the current forum experts.

I am putting the .htaccess file in the root directory.

I guess I am not sure when to invoke the 'RewriteBase" command.
I am not familiar the mod_alias, maybe this is what I need?

My goal : to have a php file with variables(?x=y) in the url resubmit itself to itself and rewrite the variables in the url(/dir/y) without ?'s, therefore showing the '?' free url in the location bar of the browser.

I will give you three different scenarios and accompanying results below.
I would give you an url to see these results but am currently tweeking to see if I can fix.
Please assume the RewriteEngine on.


Code: Select all
RewriteRule ^file/(.*)$ file.php?entry=$1[L]

error 404: 'file not found'


Code: Select all
RewriteBase /
RewriteRule ^file/(.*)$ file.php?entry=$1 [L]

Browser Pop-up: 'Redirection for this URL exceeded.
Unable to load the requested page.
This may be be caused by cookies that are blocked.'

Code: Select all
RewriteRule ^file/(.*)$ http://www.siteurl.com/file?entry=$1 [L]

Browser Pop-up: 'Redirection fo...'

interesting side note:
if I replace http.....com with %{SERVER_NAME}
then get error 404: 'file....'

My theory:
Redirect error is happening because it is looping.
Dont know why 404 error is happening.

Next step:
I think looping is good.
I think I can have code look for longer url (added directory) and stop if it is present.

edited post : replace number with file. (my test file named number.php)
I can get the url to change in the browser window, but doesnt process (404 with no Base specified.)
captain_caveman
 
Posts: 7
Joined: Sat Jan 15, 2005 12:16 pm

Postby Caterham » Mon Jan 17, 2005 2:59 am

I guess I am not sure when to invoke the 'RewriteBase" command.
I am not familiar the mod_alias, maybe this is what I need?
You can map the rest of your filesystem (outside the htdocs) to the apache webserver with an alias:

httpd.conf wrote:
Code: Select all
Alias /aliasname "/www/outside_htdocs/"
<Directory "/www/outside_htdocs">
    DirectoryIndex index.html index.htm
    Options None
    AllowOverride None
    order deny,allow
    deny from all
    allow from localhost 127.0.0.1
</Directory>

you can access the maped directory via eg localhost/aliasname/

If you're now using a .htaccess with mod_rewrite in outside_htdocs/, you've to use
Code: Select all
RewriteBase /aliasname
, because the physical filename path (outside_htdocs/) is not directly related to the URL (aliasname/).

But if you are within the regular htdocs, physical filename path=URL, you won't need to specity a Rwbase, because by default the RwBase is the corresponding filepath itself.

Code: Select all
#a space was missing before the L-Flag (may be caused by copying to the forum)
# new variale &rw=1 to prevent looping
RewriteRule ^file/(.*)$ file.php?entry=$1&rw=1 [L]



My goal : to have a php file with variables(?x=y) in the url resubmit itself to itself
This is what PHP is doing.

prevent looping (modified Rw-Rule above):
Code: Select all
<?php
ob_start();
if(!isset($rw))
{
  header("Location: http://" . $_SERVER['HTTP_HOST']
. dirname($_SERVER['PHP_SELF'])
. "file/" . ($entry));
}
ob_end_flush();
?>


Code: Select all
RewriteRule ^file/(.*)$ file.php?entry=$1[L]
error 404: 'file not found'
What URL is the reqursted URL?
Standard error-msg: The requested URL /fff was not found on this server.
Is this the correct path to your script?


if I replace http.....com with %{SERVER_NAME}
SERVER_NAME contains sometimes only domain or domain.com. You should use %{HTTP_HOST} for the full host (also without the protocoll http://) instead.

Next step:
I think looping is good.
I think I can have code look for longer url (added directory) and stop if it is present.
This is solved with the variable "rw", which only exsists if a rewrite has taken place.
Caterham
 
Posts: 690
Joined: Fri Dec 10, 2004 1:30 pm

Postby captain_caveman » Mon Jan 17, 2005 1:42 pm

Way to go Joe.
(I know your name is Bob, but it doesn't rhyme and I am trying to display extreme happy.)
You can credit yourself for one more "the day" saved. Thank You.

I tried many different combinations with the RewriteBase command not #'ed, but could only seem to get it to work with it in there.
The focus of this last sentence is, It Worked.

I will put up the code for a simple php file and the .htaccess.
It seems like alot of questions deal with this topic (especially from beginners like my self), and I think it would save time to just post a full working example.

The purpose of having a ?mark free url was to aid with search engine optimization.
By resubmitting to itself via header command and rewrite have we created something more undesireable then the ?mark url?

*Lets assume number.php and .htaccess are in the same directory.

number.php
Code: Select all
<?php
ob_start();
$entry = $_GET['entry'];
$rw = $_GET['rw'];
if (!isset($rw)) {
   header ("Location: http://" . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . "number/" . ($entry));
   ob_end_flush();
   end ();
}
ob_end_flush();
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Numb Bare Fun Bear</title>
</head>
<body>
<?php
if ($entry == null) {
   echo ("you have not entered a number");
} else {
   echo ("your number is $entry");
}
?>
<hr>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="GET">
<p>Enter Number</p>
<p><input type="text" name="entry" maxlength="32" size="22"><input type="submit" value="Find"></p>
</form>
</body>
</html>


Notice that I don't check any variables sent. This is a simple example of function, everyone can make their code secure however they want.
*If not in (htdocs) / directory, then change "number/" to "/number/".

.htaccess
Code: Select all
RewriteEngine on
RewriteBase /
RewriteRule ^number/(.*)$ number.php?entry=$1&rw=1 [L]


*if not in (htdocs) / directory, then change number.php to path/number.php.

Some of the code can probably be simplified (kind of new to php too),
but it is a good starting point that works.
I dont intend to have a page about numbers, but now that the ground work is laid more complex and exciting things can happen.

Reality is so much easier when theory passes tests, huh?
captain_caveman
 
Posts: 7
Joined: Sat Jan 15, 2005 12:16 pm

Postby Caterham » Mon Jan 17, 2005 2:20 pm

(I know your name is Bob, but it doesn't rhyme and I am trying to display extreme happy.)
Well, it's actually Robert , but sometimes I use "short versions" such as Bob or Rob :-D

The purpose of having a ?mark free url was to aid with search engine optimization.
Search engines (SE) will add the page to the index only, if they find the same (final) link somewhere on your site or on external sites.

Scenario:
--> Request from a html-Link inside your page index.php?a=1
<-- Response: 302 Found (Moved Temporarily), Location a-1.html (see below)

In this case the URL a-1.html would not be added to the index -- for 2 reasons:
>> The status code 302 is sent. The search engine will follow the new URL only, if a status code 301 (moved permanently) is send in the response - but will add the page from the response 301 Location (a-1.html) only to it's index, if HTML links are also pointing to a-1.html.
>> No links to a-1.html found on your entire site (but index.php?a=1)


To prevent this: You have to change all links within you PHP document in the <a href="..."> to the static form a-1.html (in this case). This means that the SE finds the link a-1.html and will add it to it's index.
The SE doesn't get any word that you simulate just static documents, because the RewriteRule is an internal redirect.

This is very important if your friendly URL-Layout is not just for your page visitors but for SE.

The Status code 302 with the location header is send by PHP default. To send a 301, you've to use
Code: Select all
if (!isset($rw)) {
   header("HTTP/1.1 301 Moved Permanently");
   header ("Location: http://" . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . "number/" . ($entry));
   header("Connection: close");
   ob_end_flush();
   end ();
}
ob_end_flush();
-- but again if no links are pointing to your URL you sent in the location header, the page would not be added to the SE's index.

Reality is so much easier when theory passes tests, huh?
Oh yes... :-P
Caterham
 
Posts: 690
Joined: Fri Dec 10, 2004 1:30 pm

Postby captain_caveman » Mon Jan 17, 2005 4:38 pm

Caterham wrote:Well, it's actually Robert , but sometimes I use "short versions" such as Bob or Rob :-D

My actual name is Brian, you knew via whois probably.
I would like to formally thank you very much for your guidence through this process.
Passing that second value from the redirect was great. It is amazing how the littlest thing can hang one up, then with it, the flood gates open. I was looking in to getting the url, counting the objects, etc. This didn't seem to be working because the redirect didn't actally change the url, just interped others the same. I think? Anyways, your idea was a good one and worked.

Search engines (SE) will add the page to the index only, if they find the same (final) link somewhere on your site or on external sites.

oh?
I was planning to make all my links in the form of
.../number/pi for example.
Am I correct that if I want those pages indexed, you are saying to add the
header("HTTP/1.1 301 Moved Permanently"); and
header("Connection: close"); lines to my code?

//Because the rw=1 is not included in a link of .../number/pi it will be redirected and I need correct message.
Gotcha. If that is what you are saying, then this makes sense.
Probably wouldn't hurt to throw them in anyways.

I do plan on having links to all valid entries for this page on my site.
captain_caveman
 
Posts: 7
Joined: Sat Jan 15, 2005 12:16 pm

Postby Caterham » Tue Jan 18, 2005 2:38 am

I was planning to make all my links in the form of
.../number/pi for example
They are so far static, so you shouldn't get any problems with this.

Am I correct that if I want those pages indexed, you are saying to add the
header("HTTP/1.1 301 Moved Permanently"); and
header("Connection: close"); lines to my code?
Right. The SE follows the new link send in the Location header and add the page to its index, when your site links to ../number/pi
Caterham
 
Posts: 690
Joined: Fri Dec 10, 2004 1:30 pm


Return to Friendly URLs with Mod_Rewrite

Who is online

Users browsing this forum: No registered users and 35 guests

cron