Stofke
Stofke

Reputation: 2968

redirecting static pages to dynamic while using the static filename to query

People who have bookmarked for example

http://www.dogs.bark/breeds.cfm/12700_female_rottweiler.htm

or

http://www.dogs.bark/whatevertext/12700_female_rottweiler.htm
(use female + rottweiler)

Direct this to:

http://www.dogs.bark/search/result/?q=female+rottweiler

So basically it should take the last words (can be multiple) that are separated with underscores as keywords

I understand this should be done using mod.rewrite but that's about it. I find it hard to understand how mod rewrite works.

Upvotes: 1

Views: 90

Answers (1)

Jon Lin
Jon Lin

Reputation: 143906

You can do this using mod_alias:

RedirectMatch 301 ^/.*?/[0-9]+_(.*)\.html?$ /search/result/?q=$1

Remove the 301 if you don'twant it to be a permanent redirect. You can also use mod_rewrite, it'll look pretty much the same:

RewriteRule ^/?.*?/[0-9]+_(.*)\.html?$ /search/result/?q=$1 [L,R=301]

Again, you can remove the =301 bit if you don't want a permanent redirect.


EDIT:

In order to replace the query string's _ character with + characters you'll definitely need to stick with mod_rewrite and need some extra rules for that:

# perform the initial rewrite, but don't redirect
RewriteRule ^/?.*?/[0-9]+_(.*)\.html?$ /search/result/?q=$1 [L]

# replace "_" with "+"
RewriteCond %{QUERY_STRING} ^q=([^_]*)_(.*)$
RewriteRule ^/?search/result/$ /search/result/?q=%1+%2 [L,NE]

# don't redirect until all "_" is replaced with "+"
RewriteCond %{QUERY_STRING} ^q=([^_]+)$
RewriteRule ^/?search/result/$ /search/result?q=%1 [L,R=301]

Upvotes: 1

Related Questions