Forum Moderators: phranque

Message Too Old, No Replies

page redirect

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...

WhosAWhata

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

10+ Year Member



i suppose there isn't a way to make the answer as simple as

RewriteEngine on
RewriteBase /index.php?page=/
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+[^/])$ $1/ [R]

is there?

WhosAWhata

5:47 am on Mar 29, 2004 (gmt 0)

10+ Year Member



i guess this isn't as common a question as i thought

WhosAWhata

5:22 pm on Mar 29, 2004 (gmt 0)

10+ Year Member



This isn't perfect, but i guess it works well enough

RewriteEngine on
rewriteRule (.*).html$ index.php?page=$1

my question is, is there a way to get rid of the .html?
so if they go to page1.php it will say index.php?page=page1.php

is this possible?

jdMorgan

5:35 pm on Mar 29, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It's a very common question. However, there are a large number of variants, so it is important to define exactly what you want to do.

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

This will redirect any requested resource to your script with an appropriate query string, unless it is the script itself being requested.

Jim

WhosAWhata

6:39 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:21 pm on Mar 31, 2004 (gmt 0)

10+ Year Member



is this a problem with the php code (i doubt) or the apache mod_rewrite code?

WhosAWhata

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

10+ Year Member



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

this code works perfect
except, it won't allow you to access the dir
/dir/index.php works but /dir/ returns a page not found message...any suggestions?

i was thinking about mabye adding a
RewriteCond %{REQUEST_URI}/$
RewriteRule (.*) index.php [L]
is this about right?

WhosAWhata

3:20 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

WhosAWhata

4:01 am on Apr 1, 2004 (gmt 0)

10+ Year Member



how do i also exclude subdirectories in the line
rewriteCond %{REQUEST_URI}!^.*/index\.php$
?

WhosAWhata

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

10+ Year Member



thanks