Yet Another Rewrite Rule question

software development

#1

A couple months ago I rewrote a small sub-site of mine.

Originally I had simple static pages that are still being linked to from quite a few sources. I’ve written a ‘catch-all’ rule so that the old, now-incorrect URLs at least reach the site home without erroring.

What I’ve tried several times to do is create specific rules to redirect from the static page to the new database-driven page.

I can’t seem to get the string match to work and the page-specific rules are being skipped.

For example, I have:

Redirect /Harrington/hh_shrikes_and_ferrets_are_not_fighters.htm http://infodump.thefifthimperium.com/entry/Harrington/138/1/ [L]

To make matters worse, the old site also had a generic script to strip out styling and scripts, so you could also see that same page being requested as:

Harrington/pagenocss.php?page=hh_shrikes_and_ferrets_are_not_fighters.htm

I’ve been trying to figure out how to match the substring ‘hh_shrikes_and_ferrets_are_not_fighters’, without the file extension and redirecting to the new page.

Even better would be a way to also match the presence of the ‘pagenocss.php’ script and set a switch on the target URL (the trailing /1/ would become a trailing /0/ for a script-/style-free version of the same page).

Thanks for any help you might be able to provide.

-JPB


#2

[quote]What I’ve tried several times to do is create specific rules to redirect from the static page to the new database-driven page.

I can’t seem to get the string match to work and the page-specific rules are being skipped.

For example, I have:

Redirect /Harrington/hh_shrikes_and_ferrets_are_not_fighters.htm http://infodump.thefifthimperium.com/entry/Harrington/138/1/ [L][/quote]
Huh? First of all if that line is really what you have been trying you are confusing two different methods.

The Redirect directive is handled by mod_alias and here is the reference: Apache Module mod_alias - Redirect Directive

I’ll also point this out - it only matches against the url-path and not the query string (or other variables)

[quote]To make matters worse, the old site also had a generic script to strip out styling and scripts, so you could also see that same page being requested as:

Harrington/pagenocss.php?page=hh_shrikes_and_ferrets_are_not_fighters.htm

I’ve been trying to figure out how to match the substring ‘hh_shrikes_and_ferrets_are_not_fighters’, without the file extension and redirecting to the new page.

Even better would be a way to also match the presence of the ‘pagenocss.php’ script and set a switch on the target URL (the trailing /1/ would become a trailing /0/ for a script-/style-free version of the same page).[/quote]
And for this you would need to use the more powerful Apache Module mod_rewrite. You will be able to test the value of the query string using a RewriteCond directive.

In the line you provided, you appear to mix the syntax of the RewriteRule directive with that of the Redirect directive. You can certainly issue a redirect when using mod_rewrite if you want to.

[pre]# Redirect old urls to new urls

- mod_alias: redirection based on url-path

Redirect /Harrington/hh_shrikes_and_ferrets_are_not_fighters.htm http://infodump.thefifthimperium.com/entry/Harrington/138/1/

- mod_rewrite: redirection based on url-path and query string

RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} hh_shrikes_and_ferrets_are_not_fighters
RewriteRule Harrington/pagenocss.php http://infodump.thefifthimperium.com/entry/Harrington/138/0/ [R, L]

:cool: openvein.org -//-


#3

Yes, the redirect I showed as a sample began its life as a rewrite.

As you see, I conflated the two rather badly.

Thanks for the sampler. I’ll give it a whirl.

-JPB