Map 1 URL to two pages

Discuss practical ways rearrange URLs using mod_rewrite.

Map 1 URL to two pages

Postby junaidfcs » Tue Oct 14, 2008 7:34 pm

Hi, I have an interesting problem. What I need is if the user at my site enter www.mysite/somename then it should first redirect to www.mysite/type1/type1.php?name=somename. Now if there is no record with name=somename then it should check for second type and redirect to www.mysite/type2/type2.php?name=somename. Now if still there is no record of type2 with name=somename then it should redirect to 404 page.

How can I achieve this. Can RewriteCond should be used? Please help.

Thanks
Junaid
junaidfcs
 
Posts: 3
Joined: Tue Oct 14, 2008 7:26 pm

Postby richardk » Wed Oct 15, 2008 6:40 am

Mod_rewrite can't check if your PHP script finds a result.

If you have access to the httpd.conf file, you could use a RewriteMap and a PHP script to check if a result can be found. If not, i think the only solution is to use PHP to include the second script on failure.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby junaidfcs » Wed Oct 15, 2008 7:04 am

Thanks for your reply.

Well can you please guide how I can use RewriteMap to do this. If you tell me some example then it will be a big help.

Thanks
Junaid
junaidfcs
 
Posts: 3
Joined: Tue Oct 14, 2008 7:26 pm

Postby richardk » Thu Oct 16, 2008 6:40 am

You would have something like
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteMap checktype prg:/path/to/typecheck.php

RewriteCond ${checktype:$1} ^(.+)$
RewriteCond %1 !^false$
RewriteRule ^([^/]+)/?$ %1 [QSA,L]

and then in /path/to/typecheck.php you would get the name variable (somename) in STDIN. Then you would use it to check if that name exists for each type. If it does, return the path to rewrite to (eg. /type1/type1.php?name=somename) in STDOUT or return "false" (a string not boolean) to send a 404 (the rule won't match, Apache will therefore send a 404).

Docs: RewriteMap
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby junaidfcs » Fri Oct 24, 2008 7:34 am

Hi thanks for your great help.

I tried this script but it gives me 500 internal server error every where on my server. I placed .htaccess at the root of public_html?
junaidfcs
 
Posts: 3
Joined: Tue Oct 14, 2008 7:26 pm

Postby richardk » Fri Oct 24, 2008 11:50 am

A RewriteMap must be in your httpd.conf file, not in a .htaccess file.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am


Return to Friendly URLs with Mod_Rewrite

Who is online

Users browsing this forum: Google [Bot] and 24 guests

cron