Forum Moderators: phranque

Message Too Old, No Replies

.htaccess 301 redirect + Query Strings

Really triky! Help needed

         

noface319

11:48 pm on Sep 7, 2010 (gmt 0)

10+ Year Member


Hi everyone,

I'd like to redirect some queryed Urls in to new ones (301), one by one.

I've got this structure on my "old" site:

http://www.mysite.com/path/file.php?idNumber=1
http://www.mysite.com/path/file.php?idNumber=2
http://www.mysite.com/path/file.php?idNumber=3
...

and I'd like to have one different structure for each idNumber.

http://www.mysite.com/pathA/pathB1/postName1/
http://www.mysite.com/pathA/pathB2/postName2/
http://www.mysite.com/pathA/pathB3/postName3/

I am aware that I will have to make as many conditionals as different IdNumbers.

How can I do it (I've read a lot in this forum but my english is not as great as I wish and my knowledge in Apache Mod-Rewrite is limited)

Thanks in advance.


BTW My current .htaccess is a Wordpress 3.0 multisite

RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]

# uploaded files
RewriteRule ^([_0-9a-zA-Z-]+/)?files/(.+) wp-includes/ms-files.php?file=$2 [L]

# add a trailing slash to /wp-admin
RewriteRule ^([_0-9a-zA-Z-]+/)?wp-admin$ $1wp-admin/ [R=301,L]

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*) $2 [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(.*\.php)$ $2 [L]
RewriteRule . index.php [L]

jdMorgan

1:55 pm on Sep 10, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you cannot do this in your script where it would be more efficient (because the script can "look up" the new friendly URL in your database using the old unfriendly URL as a key), then you can use a "lookup-table" approach in mod_rewrite to reduce this to a single rule, most of which won't even be processed unless the requested URL-path matches "file.php" (RewriteConds are not processed unless the RewriteRule pattern is matched).

An example of a lookup table rule for this case would be:

# Externally redirect direct client requests for old "unfriendly" URLs to new "friendly" URLs
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /path/file\.php\?idNumber=([0-9]+)\ HTTP/
RewriteCond %1>pathB1/postName1 ^1>(.+)$ [OR]
RewriteCond %1>pathB2/postName2 ^2>(.+)$ [OR]
RewriteCond %1>pathB3/postName3 ^3>(.+)$
RewriteRule ^path/file.php$ http://www.example.com/pathA/%1/? [R=301,L]

This code should only be implemented after all links on your site have been updated to refer to the new search-engine-friendly URLs. Failure to do this will likely result in the search engine's tagging your site as having poor site administration, and you will probably see many errors listed in reports from Google Webmaster Tools and similar.

This is a simple version that only handles cases where "idNumber=nnn" is the only query string parameter. A more-complex approach is needed if there are (or could be) additional parameters prepended or appended to this parameter.

The ">" as used in the RewriteConds here has no special meaning to mod_rewrite or to the regular-expressions processing engine. Its purpose here is only as a fairly-unique character to unambiguously mark the end of the idNumber and the beginning of the literal URL-path substitution string in each RewriteCond.

Jim