Click to See Complete Forum and Search --> : [RESOLVED] .htaccess redirect page to dynamic link.


CrazyGaz
07-25-2008, 05:41 AM
Hi,

I have the following .htacess to allow for well formatted URLs, so instead of index.php?pg=x, I have /page/x.


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


A couple things I would like to change are:
Replacing the /page element with a variable so index.php?pg=x&hid=y is formatted as /y/x


Redirect index.php to a 404 error so that if people try to access /z/x when z is invalid, it will give a 404 page.
I have tried:
/home.
RewriteRule ^index.php 404.html [R]

Which doesn't work.

Any help is appreciated,
Gaz.

Mr. E. Cryptic
07-25-2008, 07:17 AM
For the first one: each time you call a new field of /([^/\.]+) it is assigned the value running from $1 in order, so the first instance would be $1, the secong $2 etc.

to rewrite a value like http://www.mysite.com/y/x/

RewriteRule ^page/([^/\.]+)/([^/\.]+)/$ index.php?pg=$1&hid=$2 [L]

would rewrite http://www.mysite.com/page/y/x/ to http://www.mysite.com/index.php?pg=y&hid=x

I don't get what you're trying to do with the second one, are you trying to block access to your entire index page?

If you're trying to redirect to a 404 if page=y doesn't exist (i assume you're using an include of some desc. to add page y to the index content) then you may be better handling this in the scripting of the index page when the include statement fails.

CrazyGaz
07-25-2008, 07:39 AM
Thanks so much for the first part, works great, and I now actually understand what () does in terms of .htaccess.

For the second part, what I want to do is stop people trying to hardcode in a value for the url. So if the user attempts to go to http://site.com/page/doesnotexist/home then it will display the 404 error since there is no content related to that. However I think you are right, I may be better checking to see if what they have entered is valid and then change the headers to redirect to the 404 page.

Thanks again,
Gaz.