Daniel Cook
Daniel Cook

Reputation: 1043

SEO URLs with ColdFusion controller?

quick ref: area = portal type page.

I would like old urls http://domain.com/long/rubbish/url/blah/blah/index.cfm?id=12345 to redirect to http://domain.com/area/12345-short-title

http://domain.com/area/12345-short-title should display the content.

I have worked out so far to do this I could use apache to write all URLs to

http://domain.com/index.cfm/long/rubbish/url/blah/blah/index.cfm?id=12345 and http://domain.com/index.cfm/area/12345-short-title

The index.cfm will either server the content or apply a permanent redirect, but it will need to get the title and area information from the database first.

There are 50,000 pages on this website. I also have other ideas for subdomain redirects, and permanent subdomains and controlling how they act through the index.cfm.

Infrastructure are keen to do as much through Apache rewrite as possible, we suspect it would be faster. However I'm not sure we have that choice if we need to get the area and title information for each page.

Has anyone got some experience with this that can provide input?

--

Something to note, I'm assuming we'll have to keep all the internal URLs used on the website in the old format. It would be a mega job to change them all.

This means all internal URLs will have to use a permanent redirect every time.

Upvotes: 4

Views: 929

Answers (2)

Peter Boughton
Peter Boughton

Reputation: 112160

Rather than redirecting both groups of URLs to the same script, why not simply send them to two distinct scripts?

Simply like this:

RewriteCond ${REQUEST_URI}  !-f
RewriteRule ^\w+/\d+-[\w-]+$ /content.cfm/$0 [L]

RewriteCond ${REQUEST_URI}  !-f
RewriteRule ^.* /redirect.cfm/$0   [L,QSA]

Then, the redirect.cfm can lookup the replacement URL and do the 301 redirect, whilst content.cfm simply serves the content.

(You haven't specified how your CF is setup; you may need to update the Jrun/Tomcat/other config to support /content.cfm/* and /redirect.cfm/* - it'll be done the same as it's done for index.cfm)


For performance reasons, you still want to avoid the database hits for redirecting if you can, and you can do that by generating rewrite rules for each page that performs the 301 redirect on the Apache side. This can be as simple as appending a line to the .htaccess file, like so:

<cfset NewLine = 'RewriteRule #ReEscape(OldUrl)# #NewUrl#   [L,QSA,R=301]' />

<cffile action="append" file="./.htaccess" output=#NewLine# />

(Where OldUrl and NewUrl have been looked-up from the database.)

You might also want to investigate using mod_alias redirect instead of mod_rewrite RewriteRule, where the syntax would be Redirect permanent #OldUrl# #NewUrl# - since the OldUrl is an exact path match it would likely be faster.

Note that these rules will need to be checked before the above redirect.cfm redirect is done - if they are in the same .htaccess you can't simply do an append, but if they are in the site's general Apache config files then the .htaccess rules will be checked first.

Also, as per Sharon's comment, you should verify if your Apache will handle 50k rules - whilst I've seen it reported that "thousands" of regex-based Apache rewrites are perfectly fine, there may well be some limit (or at least the need to split across multiple files).

Upvotes: 3

Sharondio
Sharondio

Reputation: 2614

Using apache rewrites would only be faster if they were static rewrites, or if they all followed some rule that you could write in regex within the .htaccess file. If you're having to touch the database for these redirects, then it may not make sense to do it in .htaccess.

Another approach is the one used by most CMSs for handling virtual directories and redirects. An index.cfm file at the root of the site handles all incoming requests and returns the correct pages and pathing. MURA CMS uses this approach (as well as Joomla and most of the others.)

Basically you're using the CGI.path_info variable on an incoming request, searching for it in your DB, and doing a redirect to the new path. As usual, Ben Nadel has a good write-up of how to use this approach: Ben Nadel: Using IIS URL Rewriting And CGI.PATH_INFO With IIS MOD-Rewrite

You can, however, use the .htaccess to remove the "index.cfm" from the url string entirely if you want by redirecting all incoming requests to the root URL with something that looks like this in your .htaccess:

RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-d
RewriteRule ^([a-zA-Z0-9-]{1,})/([a-zA-Z0-9/-]+)$ /$1/index.cfm/$2 [PT]

Basically this would redirect something like http://www.yourdomain.com/your-new-url/ to http://www.yourdomain.com/index.cfm/your-new-url/ where it could be processed as described by the blog post above. The user would never see the index.cfm.

Upvotes: 0

Related Questions