homepage Welcome to WebmasterWorld Guest from 50.17.27.205
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Problems with url rewrite
Odd output. What's wrong?
neiljones




msg:1315310
 4:44 pm on Oct 1, 2004 (gmt 0)

I am trying to get

[example.com...]

to be activated from
[/www.example.com/test/something...]

Here is the index.php file

<html>
hello
<?php
$count=1;
$factor=$_GET['factor'];
print "TTT".$factor."HHH";
?>

here is the .htaccess file

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} ^\/([^\/]+)/?
rewriteRule .* test.php?factor=%1 [L]

Here is the output

<html>
hello
usr

Where is the usr coming from?
What am I doing wrong?

[edited by: ergophobe at 6:41 pm (utc) on Oct. 1, 2004]
[edit reason] URL exemplified (unintentionial commercial domain in original version) [/edit]

 

ergophobe




msg:1315311
 6:44 pm on Oct 1, 2004 (gmt 0)

the 'usr' is coming from the fact that your file is probably something like

usr/home/htdocs/index.php

You match until the the first '/' and then capture that as a backreference and assign it to factor, which you echo out. So you know mod_rewrite is working properly - that's something! I'll look at the rest and try to come up with the right rule, but that's not my thing... it usually takes me a few tries.

Tom

jdMorgan




msg:1315312
 6:53 pm on Oct 1, 2004 (gmt 0)

> www.example.com/test/something -> www.example.com/test/index.php?factor=something

How about just using:

RewriteEngine on
RewriteRule ^test/([^/]+) index.php?factor=$1 [L]

This avoids having to parse out the local filepath.

Jim

ergophobe




msg:1315313
 7:09 pm on Oct 1, 2004 (gmt 0)

I was assuming that "test" is not constant, so you have to capture from the example.com onwards.

Basically, I just do this,

RewriteEngine on
RewriteRule ^(.*)index.php$ - [L]
RewriteRule!(.*)\.(gif¦jpg¦png¦css¦js¦doc¦pdf)$ index.php [L]

The second line keeps you from getting caught in a loop.
The third line prevents rewriting of requests for documents with those extensions.

Once within the PHP script, I have full access to the URL and can use all the power of PHP to chop it up any way I want. That works very well for me.

Tom

neiljones




msg:1315314
 5:24 pm on Oct 3, 2004 (gmt 0)

Thanks for your help guys but I'm sorry but nothing seems to be working for me still.

I have played with the above suggestions and the original until I am pulling my hair out
with no luck.
Tom's suggestion seems promising
The first two lines

RewriteEngine on
RewriteRule ^(.*)index.php$ - [L]

Seem to work when I try
www.example.com/test/
In that I get the PHP program activated but there is of course no data
But when I try www.example.com/test/somedata/
the browser tells me the file does not exist. Of course it doesn't so it seems that the rewriting isn't working.

adding the third line

RewriteRule!(.*)\.(gif¦jpg¦png¦css¦js¦doc¦pdf)$ index.php [L]

Gives me a server error.

Perhaps a little explanation of what I am ultimately trying to do will help.
This is part of a large site and I am turning part of it into a dynamic site.
This part is a large photo gallery and I can simple have a list of the photographs in a file and
create an individual page dynamically from the file. Rather than my present approach which creates a single page for every
photograph which makes changing things very difficult.

I need an piece of code that will enable me to use it in

www.example.com/directory1/
www.example.com/directory2/
and so on.

ergophobe




msg:1315315
 5:46 pm on Oct 3, 2004 (gmt 0)

With my method, you need to parse everything out within php, so you would need to do something like this to start with

// create a URL without any GET params and don't allow HTML or PHP in URI
$url = strip_tags(str_replace("?" . $_SERVER['QUERY_STRING'], "", $_SERVER['REQUEST_URI']));

// we don't want the server name part b/c we don't store that in our DB (portability)
$url = str_replace($_SERVER['SERVER_NAME'], '', $url);

now the $url should be the same as the one we store in our DB or lookup file.

Query the DB for the $url and get the content of the page or the filesystem path to the image or whatever it is that you do. So in your case, the $url part would be equal to 'test/somedata/'

$query = SELECT photo_title as title, photo_caption as caption, photo_src as src FROM photos WHERE photo_url='$url'";

and then, once you've fetched that data from the DB, on your template page you would have something like

<title><?=$photo['title'];?></title>
...
<div class="photo">
<img src="<?=$photo['src'];?>">
<p class="caption"><?=$photo['caption'];?>"></p>
</div>

This is sort of nice because the url is completely independent of the file system. In addition, you can still have URLs where the GET parameters look like get parameters if you want that, but can look like "friendly urls" if you want, which I sometimes find useful.

To be honest, I also sometimes find it wasteful, since a rewrite rule adds almost no server overhead, whereas this method adds a database request and additional scripting. If you can do what you want simply with a simple rewrite rule, that is undoubtedly the better solution. From what you describe, though, I think you might find you get some decent benefit from the more complex method. It depends a little on your traffic, server power and all that I guess.

The rule that causes a server error is probably doing so because you cut and pasted it. This forum software breaks pipes, so when you paste code wth pipes in it, you must retype the ¦ Sorry I forgot to mention that when I posted the original rule.

Tom

neiljones




msg:1315316
 6:49 pm on Oct 3, 2004 (gmt 0)

Yes Tom,
That is exactly the kind of approach I was thinking of, but I am still encountering dificulty

For the moment this is the .htaccess file I am using

RewriteEngine on
RewriteRule ^(.*)index.php$ - [L]

and I have rewritten the PHP a little

<html>
hello<br>
<?php
$factor=$_GET['factor'];
print "URI ".$_SERVER['REQUEST_URI']."<br>\n";
$url = strip_tags(str_replace("?" . $_SERVER['QUERY_STRING'], "", $_SERVER['REQUEST_URI']));
print "URL ".$url."<BR>\n";
?>

[example.com...]

gives me

hello
URI /test/
URL /test/

[example.com...]

gives me

hello
URI /test/?a
URL /test/

and

[example.com...]

gives me

Not Found

The requested URL /test/a was not found on this server.

I only put in the?a as an accident originally but as it produced a different result I enclude it.

Surely I should be processing the /test/a through the PHP program, but clearly I am not.

Any idea what is wrong?

ergophobe




msg:1315317
 8:11 pm on Oct 3, 2004 (gmt 0)

Assuming that index.php is in the /test directory, then it appears that you are not rewriting the URL. Are you sure that mod_rewrite works?

neiljones




msg:1315318
 9:36 pm on Oct 3, 2004 (gmt 0)

Well my first set of results indicated that it did work.

Anyway going back to that and thinking about it.
I chaanged the .htaccess to

RewriteEngine on
rewriteRule .* index.php?factor=%1 [L]

Now here is a piece of sample test output. It is working!

[example.com...]
gives

hello
URI /test/aa/blogs/nnn/nnn/
URL /test/aa/blogs/nnn/nnn/

ergophobe




msg:1315319
 12:30 am on Oct 4, 2004 (gmt 0)

Wasn't paying attention. I thought you were just simplifying my filter for gifs and pdfs. I didn't notice that you were using the previous rule.

This rule

RewriteRule ^(.*)index.php$ - [L]

merely says that if the URL ends with 'index.php', stop rewriting, don't do anything, quit. So naturally it isn't working for you.

If you were to put the other rule back in, with the pipes retyped, it would work

RewriteRule!(.*)\.(gif¦jpg¦png¦css¦js¦doc¦pdf)$ index.php [L]

If you don't care about gifs and so on and you want everything rewritten,

RewriteRule .* index.php [L]

would also work just fine. your '?factor=%1' isn't doing anything.

neiljones




msg:1315320
 5:30 pm on Oct 6, 2004 (gmt 0)

Thanks for all your help.

However I am still puzzled why your option does not work for me. It is what I need since I have JPEGs in the directory.

I tried retyping the pipes. REsult server error.
As I only needed to exclude JPEGs, as all the gifs in the design can be stored elsewhere.

I changed it as follows. Pasted straigh from the file

RewriteEngine on
RewriteRule ^(.*)index.php$ - [L]
RewriteRule!(.*)\.(jpg)$ index.php [L]

Rsutul still a
server error

What Am I doing wrong?

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved