Forum Moderators: phranque

Message Too Old, No Replies

RewriteRule to centralized index, redirects and SEO URI's

         

tunafish

1:59 pm on Feb 11, 2010 (gmt 0)

10+ Year Member



I am making a site with photo albums for agents.
Each agent dir has unique numeric album folders containing an index.php file that redirects to a centralized album.php like this:
RewriteRule ^[^/]+/[^/]+/index\.php$ albumpage/album.php (I should make the second argument numeric though)

mysite.com/
mysite.com/agent1/
mysite.com/agent1/005/
mysite.com/agent1/005/index.php
mysite.com/agent1/006/
mysite.com/agent1/006/index.php
mysite.com/agent2/
mysite.com/agent2/009/
mysite.com/agent2/009/index.php
...
mysite.com/albumpage/
mysite.com/albumpage/album.php


Now I would like to map shortcuts:
mysite/005 redirects to mysite/agent1/005
mysite/009 redirects to mysite/agent2/009

Since I will have lots of agent and album folders this needs to be dynamic.
I understand I should send URI requests to a php script, tied to a DB, which handles the 301
mysite.com/redirect.php?id=005

Another think I would love is SEO URI's:
mysite/005 redirects to mysite/agent1/005-some-nice-keywords
mysite/agent1/005 redirects to mysite/agent1/005-some-nice-keywords

Is this possible? Thanks.

jdMorgan

3:21 pm on Feb 11, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I strongly recommend that you do this with your script(s) -- or with another script "wrapped around" your existing script(s).

There is no way that mod_rewrite can 'know' that /agent1/005 is associated with "some-nice-keywords", so your scripts will need to be involved anyway, and there is little use in scattering the parts of your URL-management among both .htaccess and your scripts.

Should you decide to proceed on the .htaccess method, you must answer this question first: What part of the requested URL-paths linked-to by your pages will be used to determine if the requested 'friendly' URL refers to an "agent" file or to an "album" file? This information must be provided in the URL requested by the client, or the server will not know which 'kind' of page the client wants.

Be aware of the important difference between URL-to-URL redirects, and URL-to-filepath rewrites -- I always use the somewhat-redundant terms "external redirects" and "internal rewrites" to make this distinction clear.

The 'target' of an external redirect is a URL, a redirect requires a second HTTP transaction with the client, and a redirect is visible to that client (the visitor sees a new URL in his/her browser address bar).

The target of an internal rewrite is a file, and this internal URL-to-filepath translation is not visible to the client.

Also be aware of the 'direction' of your redirecting and rewriting. You likely want to rewrite requests for the "/agent1/005-some-nice-keywords" to a filepath like /agents.php?agent=005&kws=some-nice-keywords, and once that is working, you *may* wish to redirect only direct client requests for the filepath "/agents.php?agent=005&kws=some-nice-keywords" back to the URL example.com/agent1/005-some-nice-keywords. But this second step would be entirely optional, and serve only to speed up re-indexing of your site by search engines and to prevent duplicate-content issues; It would play no part in actually accomplishing the main goal.


Jim

tunafish

6:03 pm on Feb 11, 2010 (gmt 0)

10+ Year Member



Thanks for the reply Jim.
This is all way above my head though, I am going to have to drop the whole SEO part.

Could you just show me how to make the second argument in my rewrite rule numeric, so it works for my /agent/000/ structure? Cheers

RewriteRule ^[^/]+/[^/]+/index\.php$ albumpage/album.php

What about the redirects? Is it a bad idea?

jdMorgan

6:55 pm on Feb 11, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




RewriteRule ^agent[0-9]+/[0-9]+/(index\.php)?$ albumpage/album.php [L]

This rewrites the "agent" requests you showed in your first post --"agent<digit>/<digits>/<optional-index.php>"-- to albumpage/album.php. I presume that this script pulls the originally-requested URL from a server variable to figure out what content to serve.

The redirect is not "a bad idea" at all. It just can't be done with mod_rewrite, because there is no way for mod_rewrite to 'know' what the 'nice-keywords-here' string assoicated with the "agent" and "agent-number" should be; You need database access to look that up.

Jim

tunafish

11:37 am on Feb 12, 2010 (gmt 0)

10+ Year Member



Yeah thanks.
I just read something about a RewriteMap

abc 123
def 456
ghi 789

RewriteMap name2id txt:/path/to/map.txt
RewriteRule ^/files/([^/]+)\.html$ /file.php?id=${name2id:$1|0} [QSA,L]

But I will use a database anyway.

jdMorgan

3:23 pm on Feb 12, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The advantage of using your script is that you won't have to duplicate the URL-related "nice-keywords-here" from your original database into the RewriteMap txt file and then maintain both "databases" from that point forward.

You could write a small separate PERL script to query the database and call that script from a RewriteMap, but that strikes me as duplicated effort and maintenance. Also, should this map script ever "die," your site would essentially be shut down until you restarted it -- The requirement for robust script-coding in this kind of application is extremely-high; The map script must handle *all* possible errors gracefully, and must never, ever be allowed to "die."

Defining a RewriteMap also requires access to the server configuration, and so is not an option for most Webmasters on shared hosting. Once defined, however, RewriteMaps may be called from a .htaccess context.

Jim