Rewrite only if found on database

Discuss practical ways rearrange URLs using mod_rewrite.

Rewrite only if found on database

Postby agucho » Fri Jan 23, 2009 2:41 am

Hi, I have a site with this type of URLs: http://subdomain.example.com/title-of-an-article

I have to redirect (301) a few thousands of these to another application in which URLs look like: http://example.com/category/title-of-an-article/1234

When a request arrives I use a RewriteMap prg with a php script to lookup the 'title-of-an-article' on a mysql table and obtain the new URL in the form 'category/title-of-an-article/1234'. I add the 'http://example.com' and spit it back to apache to issue the 301 redirect. That works well.

Now, my problem is that not all URLs need to be redirected and there's no way to tell from the URL itself. The logic to rewrite or not is based on whether the URL is found or not in the database... but when it's not found I can't redirect to the same URL because it would go into a loop... So, how do I tell apache to serve the originally requested URL at this point?

I am very new to mod_rewrite so I don't even know if I'm on the right track here. Any help will be appreciated.
agucho
 
Posts: 3
Joined: Fri Jan 23, 2009 1:28 am

Postby agucho » Fri Jan 23, 2009 4:57 am

OK, I figure it out... I guess I didn't understand how mod_rewrite works. Just ignore my previous post.
agucho
 
Posts: 3
Joined: Fri Jan 23, 2009 1:28 am

Postby richardk » Fri Jan 23, 2009 11:31 am

Could you post (or explain) you solution for anyone else with a similar problem?

I think you will probably have made it redirect only if the RewriteMap returns a new URL. When a new URL is not found it would send back a constant value which could be checked for and the rewrite stopped.
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteMap urlredircets prg:/path/to/prg

# The default value is "false" and the program should
# return "false" when the URL is not found in the database.
RewriteCond ${urlredirects:$1|false} ^(.+)$
# If it's "false" don't redirect.
RewriteCond %1 !^false$ [NC]
RewriteRule ^/(.+)$ http://example.com%1 [R=301,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby agucho » Mon Jan 26, 2009 1:35 am

I actually did something a lot more cumbersome. It works but I'll change it to yours.

Instead of checking for false I have the script return the original URL prepending a @no-redirect@ 'mark' that I match but not capture in the next rule (so I can rewrite it for friendly URLs too) and then any URLs that come out of the script without the mark I redirect to the new app.

I think what I got mixed up with was that I used the call to the script in the RewriteRule instead of in the RewriteCond so when the script didn't find a URL in the db, I needed to return something...

Code: Select all
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ${get-new-url:$1} [NE]
RewriteRule ^(?:@no-redirect@)(.*)$ index.php?q=$1 [L,QSA]
RewriteRule ^(.*)$ - [R=permanent,L]


Anyway, I'm changing it to yours. Thank you for your help.
agucho
 
Posts: 3
Joined: Fri Jan 23, 2009 1:28 am


Return to Friendly URLs with Mod_Rewrite

Who is online

Users browsing this forum: No registered users and 20 guests

cron