pedrohd
pedrohd

Reputation: 1

301 redirect from dynamic URL in subfolder to a new static URL

I reviewed all the different solutions for redirecting dynamic URLs to a static URL but I couldn't find any case related with my question.

I would like to know what should be the correct 301 redirect rule for .htaccess

Additional info:

Here is the case I want to redirect OLD Dynamic URLs to their individual static URLs.

Old URL = http://example.com/desarrollo/mantenedores/art_indice.asp?art_id=61

New URL = http://example.com/comunidad/articles/2/tipos-de-sociedades

Thanks...

Upvotes: 0

Views: 632

Answers (1)

Olivier Pons
Olivier Pons

Reputation: 15796

Please try to fill a rewritemap file (see here) to make a correspondance with the URL destination.

Create a mapfile where you put all the categories you need:

RewriteMap mapoldtonew \
  dbm:/web/htdocs/yoursite/rewriterules/mapoldtonew.map

Regarding your sample, your map file may be filled with things like:

art_id=61        articles/2/tipos-de-sociedades
...

Then here you go for the hard part:

RewriteCond %{REQUEST_URI} desarrollo/(.*)
RewriteCond %{QUERY_STRING} (([^=]+)=([0-9]+))
# The following rule doesn't touch the URL, but 
# will try to search into the map file and
# create an OLDTONEW environment variable with the string found
# and if not found, assign OLDTONEW to "notfound"
RewriteRule . - [QSA,E=OLDTONEW:${mapoldtonew:%1|notfound}]

# if the OLDTONEW is not empty and is not found:
RewriteCond %{ENV:OLDTONEW} notfound
# this should never happen => 404:
RewriteRule . - [R=404,L]

# Reach here means :
# - OLDTONEW is empty
# - OLDTONEW is was found
# => if OLDTONEW is not empty:
RewriteCond %{ENV:OLDTONEW} !^$
# make the redirection to the corresponding found:
RewriteRule . /%{ENV:CATEGORYREVERSE} [QSA,L]

This should work. If it doesn't try to change %1 to %2 in the first RewriteRule directive. If it still doesn't you may have enough clues to finish the job. If you don't have enough clues...

Two hints:


Please try to use the RewriteLog directive: it helps you to track down such problems:

# Trace:
# (!) file gets big quickly, remove in prod environments:
RewriteLog "/web/logs/mywebsite.rewrite.log"
RewriteLogLevel 9
RewriteEngine On

My favorite tool to check for regexp:

http://www.quanetic.com/Regex (don't forget to choose ereg(POSIX) instead of preg(PCRE)!)

Upvotes: 1

Related Questions