mod_rewrite - dumb question!

Hi all,

Need a bit of help. We’ve moved CMS platforms recently and are using mod_rewrite to 301 redirect old URL’s to new ones.

The old URL’s follow this format:

/article.php/172

However they could have other things such as

/article.php/172/2
or
/article.php/172/2/30

The only bit that is of interest to us right now is the ID after the first slash (i.e. 172 from the above example).

At present we have a rewrite rule that says the following:

RewriteRule ^article\.php\/172 “http\:\/\/www\.somedomain\.com\/\?p\=4332” [R=301,L]

That doesn’t seem to work as when it is matching against the rule if we use /article.php/172 in the URL it seems to match against /article.php/17 and stop processing…

If we use the following rule:

RewriteRule ^article\.php\/172$ “http\:\/\/www\.somedomain\.com\/\?p\=4332” [R=301,L] (i.e. with a $ after the 172)

We find that it matches perfectly if the URL is just /article.php/172 but if it is /article.php/172/2/30 (or anything with more parameters after 172) it sends those through to the new URL and that causes all sorts of issues to not match.

What we want is a rule that matches on /article.php/[id#] and ignores everything else after it.

Any advice?

Many thanks!

SW,

Only the ID, eh? The obvious question is what do you want done with any extraneous information?

Also, you’ve gone crazy with unnecessary (and incorrect) escaping of characters in the regex and there should be NO escaping OR quotes (for regex purposes) in the redirection, i.e.,

RewriteRule ^article\\.php[COLOR="#FF0000"]\\[/COLOR]/172 [COLOR="#FF0000"]"[/COLOR]http[COLOR="#FF0000"]\\[/COLOR]:[COLOR="#FF0000"]\\[/COLOR]/[COLOR="#FF0000"]\\[/COLOR]/www[COLOR="#FF0000"]\\[/COLOR].somedomain[COLOR="#FF0000"]\\[/COLOR].com[COLOR="#FF0000"]\\[/COLOR]/[COLOR="#FF0000"]\\[/COLOR]?p[COLOR="#FF0000"]\\[/COLOR]=4332[COLOR="#FF0000"]"[/COLOR] [R=301,L]

ARGH!

IF you mean to specify only 172 (and discard any “trailers”), then add a / after the 172. Your example should work with 172$ as you’ve shown but ONLY if there are no other “trailers”) - the / should allow a match and redirection for you.

IF there are no “trailers,” then use $ to terminate the {REQUEST_URI} string.

IF you want to keep all the “trailers,” how are they to be handled? are they to be assigned to (optional?) other keys? What are those keys?

A generic rule? Same as above but match (\d+) and use $1 in the redirection. I would expect that, given your example, though, you don’t have a one-to-one match of the ID and the value of the key “p” so either a long .htaccess (always a BAD idea), a RewriteMap (but only if you have access to the server’s configuration file - because you can easily bring the server down with any error) or a handler file to make sense of your mapping of the id to the value for p.

Regards,

DK

DK,

Thanks for the response. Not very good at this mod_rewrite stuff :slight_smile:

So - thanks for the detail. To answer a few questions - i’ve got a one-to-one mapping on the old ID (i.e. 172 in the above example) and the value of the p parameter on the new url - at the moment i’ve got about 4000 mappings I need to do which equates to a large .htaccess file (am happy to hear other ways of doing it). It’s a dedicated server so I can do whatever is needed. As for the original URL - all I care about is the first ID value - the rest can just be thrown away. Basically I want to match on the first ID, ignore everything else and redirect to a specific alternative URL.

Does that make sense?

Cheers!

Stephan,

Very clearly, 4000 mappings is not acceptable for an .htaccess file.

A RewriteMap (enabled in the vhosts or server config files) will only be read once and is the optimum way to go.

The safest way to go is to use a handler file which can access a database table to make a simple redirection via a PHP redirection (using the header() function).

Regards,

DK