FRIHOSTFORUMSSEARCHFAQTOSBLOGSCOMPETITIONS
You are invited to Log in or Register a free Frihost Account!


mod_rewrite help





alalex
Could it be possible using mod_rewrite to redirect non existing web pages to the search file, so that whenever they search for a page that does not exist they would see search results?

For example, if someone tries to type in a wrong page say http://yourserver.com/pagethatdoesntexist.html you redirect it w/ mod_rewrite to http://yourserver.com/search.php?query=pagethatdoesntexist so that you come up w/ links to possibly what the user was looking for instead of a ‘page not found’.

thanks
Fire Boar
Uh... the one way I can think of doing this is to rewrite everything to a php file, which checks for the page in question and outputs it if it exists, giving the search page if not.

e.g.

Input: http://www.yourpage.com/pagethatexists.html
mod_rewrite changes it to: http://www.yourpage.com/getpage.php?page=pagethatexists.html

getpage.php:

Code:
<?php
if (file_exists($_GET['page']))
{
    require($_GET['page']);
}
else
{
    require('search.php');
}
?>


search.php would then use the $_GET['page'] variable as the parameter for its search.
MrBlueSky
Try this:

Code:

RewriteEngine on
RewriteCond  /<yourroot>/%{REQUEST_FILENAME} !-f
RewriteRule   ^(.+) http://yourserver.com/search.php?query=$1


Where <yourroot> is the absolute path of your documentroot (on frihost this is /domains/<site>.frih.net/public_html/).
alalex
Thanks a lot, now, I already have some rewrite engine stuff set up, so should I just paste it afterwards or there is a way of doing this?

here is the contents of my .htaccess:
Code:
ErrorDocument 401 /errorDoc.php?content=atzr
ErrorDocument 404 /errorDoc.php?content=nenctn
ErrorDocument 500 /errorDoc.php?content=arrintr
# this is the initialization
Options         +FollowSymLinks
RewriteEngine   On

# these are the rewrite conditions
RewriteCond     %{REQUEST_FILENAME}     !-f
RewriteCond     %{REQUEST_FILENAME}     !-d

# and finally, the rewrite rules
RewriteRule     ^([a-zA-Z0-9\-]+)/?$    /userinfo.php?user=$1 [L,QSA]

Those are the error docs, and then what I use the following is to redirect usernames to the userinfo.php page.

How should I implement the code you gave me?
thanks Wink
MrBlueSky
I'm not sure I understand. It looks like you are already directing non-existing pages to userinfo.php. If so, putting another rewrite for non-existing pages after it is useless, because it will not be used.
alalex
No no, Im sorry if I didnt explain correctly.
The thing is that it is redirecting this:
kdams.es/alex
To
kdams.es/userinfo.php?user=alex
So that the user page is displayed directly.

What I wanted to do is that pages contaning a format (.php) in case they dont exist, have them redirected to the search page and in the query put the name of the non existing file...
But I was thinking that I could also use the current error system to redirect to search.php and once there use the php referrer as a query...

Thanks anyways, I really appreciate it Very Happy
Related topics
can i help?
help,Bonding
HELP!!
Help me with MySQL Account Maintenance and phpBB 2.0.15 :((
mod_rewrite
help, change pass my acc in cpanel
help me upgrade php to 5.
help with mod_rewrite
Apache mod_rewrite
need help with mod_rewrite rules
Need help with mod_rewrite
Need help with file permissions for WordPress & plugins
Struggling with mod_rewrite
HELP in .htaccess file modified
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.