Forum Moderators: coopster

Message Too Old, No Replies

redirect page

same old question

         

WebNeeds

10:10 pm on Mar 27, 2004 (gmt 0)

10+ Year Member



I'm sure this is a question that everyone has heard a thousand times
I'm posting in PHP and Apache because it uses both techniques

i need a script that will redirect a .html to a?page=

examples:

site.com/pictures.html to site.com/index.php?page=pictures.html

site.com/this.php to site.com/index.php?page=this.php

i think it uses mod_rewrite but i've never used it before. does anyone have an appropriate script?

thank you in advance for any help...

mykel79

11:04 pm on Mar 27, 2004 (gmt 0)

10+ Year Member



Is not really a script in PHP you will use. You have to enter a line in the Apache configuration file that will tell it to carry out this redirect. It depends on your server configuration, but you might be able to simply put it in a file called .htaccess in the directory you want the redirects to take place.
Many useful examples here:
[httpd.apache.org...]

[edited by: jatar_k at 11:58 pm (utc) on Mar. 27, 2004]
[edit reason] linked up url [/edit]

WhosAWhata

6:23 am on Mar 28, 2004 (gmt 0)

10+ Year Member



wow,
i was looking for a similar script...actually that same exact thing

none of that stuff makes any sense to me
hope you have more luck than i do...let me know if and when you make the script

jatar_k

4:40 pm on Mar 28, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



maybe try this one
An Introduction to Redirecting URLs on an Apache Server [webmasterworld.com]

WhosAWhata

6:41 pm on Mar 29, 2004 (gmt 0)

10+ Year Member



I have a problem with this code, these are the source codes:

// .htaccess
RewriteEngine on
RewriteCond %{REQUEST_URI}!^index\.php$
RewriteRule (.*) index.php?page=$1 [L]

// index.php
echo "this is the index<body bgcolor=black text=white><br>";
if ($page!= "") { include("$page"); }

// test.php
echo "this is test.php";

if i goto:

// server.com/folder
The page cannot be found

// server.com/folder/test.php
this is the index
this is the index
this is the index
this is the index
this is the index...

// server.com/folder/index.php
this is the index
this is the index
this is the index
this is the index
this is the index...

WhosAWhata

5:20 pm on Mar 31, 2004 (gmt 0)

10+ Year Member



i'm just wondering if this is a php or a apache problem

mykel79

6:02 pm on Mar 31, 2004 (gmt 0)

10+ Year Member



It's a problem with the rewrite rule. The URI is not just index.php, but contains the directory name as well I think. When the redirect takes place to index.php, the rule gets applied again. And again, and again..:)

Try this:
RewriteEngine on
RewriteCond %{REQUEST_URI}!^.*/index\.php$
RewriteRule (.*) index.php?page=$1 [L]

Seems to work on my server.

WhosAWhata

6:30 pm on Mar 31, 2004 (gmt 0)

10+ Year Member



RewriteEngine on
RewriteCond %{REQUEST_URI}!^.*/index\.php$
RewriteRule (.*) index.php?page=$1 [L]

Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator, admin@___.com and inform them of the time the error occurred, and anything you might have done that may have caused the error.

More information about this error may be available in the server error log.

--------------------------------------------------------------------------------

Apache/1.3.29 Server at www.___.com Port 80

mykel79

8:35 pm on Mar 31, 2004 (gmt 0)

10+ Year Member



Make sure there's a space between } and!
The scripts on this forum edit it out.

WhosAWhata

9:55 pm on Mar 31, 2004 (gmt 0)

10+ Year Member



PERFECT

thanks

WhosAWhata

2:14 am on Apr 1, 2004 (gmt 0)

10+ Year Member



it works for the most part, but my friend pointed out an obvious problem:
it is in a folder right now (for testing) but it will be in my root dir.
if i access:
/dir/index.php it works
/dir/test.php it works
/dir/
The page cannot be found
The page you are looking for might have been removed, had its name changed, or is temporarily unavailable.

--------------------------------------------------------------------------------

HTTP 400 - Bad Request
Internet Explorer

sorry, but what code do i add to allow it to automatically go to the index

WhosAWhata

3:19 am on Apr 1, 2004 (gmt 0)

10+ Year Member



i fixed it, here is the completed code

RewriteEngine on  
RewriteCond %{REQUEST_URI}!^.*/index\.php$
RewriteRule (.*) index.php?page=$1 [L]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule (.*) index.php [L]

i also changed the include() function to include_once() to fix errors caused by accessing dir/index without the extention

P.S. you can make it so the scripts don't filter spaces using the [ pre ] style code

mykel79

10:15 am on Apr 1, 2004 (gmt 0)

10+ Year Member



Glad you finally got it working. I'll try pre next time I post something, thanks.