Forum Moderators: phranque
www.mydomain.com/?p=1
www.mydomain.com/?p=2
etc...
I'm moving to a new WAMP server running drupal 4.7.5 which does support mod_rewrite. i will be manually moving old content to drupal. The new url format will be something like this:
www.mydomain.com/some-category/the-article-title.html
My question is, what is the best way to handle 301 redirects to the new content. I've been reading up on mod_rewrite for about a week now and can't really find anything that works well.
This works for redirecting one instance, but since i have a hundred or so, two lines for one redirect seems a little much:
RewriteCond %{query_string} ^feed=rss2&cat=34$
RewriteRule ^/$ /feed/? [R=301,L]
But i came across this and thought that it may work for all my?p= variables.
RewriteMap pages txt:pids.txt
RewriteRule ^(.*)$?p=${pages:$1¦0} [R=301,L]
pids.txt:
testing/test 1
testing2/test2 2
when i use the above RewriteMap and RewriteRule, and restart apache, apache doesn't start and gets hungup on "RewriteMap pages txt:pids.txt". I have tried /pids.txt and pids.txt (the text file is located in the public webroot, and this is a windows server running apache so i'm a little confused.
Any help would be greatly appreciated.
I assume that the feed name is always "rss" and that it is only the category that changes. However, you can probably adapt this example if that's not true.
RewriteMap pages txt:/same-path-as-used-for-document_root/pids.txt
#
RewriteCond %{QUERY_STRING} ^feed=rss&cat=([0-9]+)$
RewriteRule ^/$ http://www.example.com/${pages:%1¦NULL} [R=301,L]/path_to_document_root/pids.txt:
1 category-1/article-2.html
2 category-1/article-3.html
3 category-2/article-7.html
19 category-3/article-21.html
The intent here is to redirect the old dynamic URLs to the new static ones, and then let the existing CMS internal rewrites take care of them just as they would if the new static URL was directly requested.
Jim
RewriteMap pages txt:c:/wamp/Apache2/conf/pids.txt
RewriteCond %{QUERY_STRING} ^p=([0-9]+)$
RewriteRule ^/$ http://www.example.com/${pages:%1¦NULL} [R=301,L]
pids.txt
1 category-1/article-2.html
2 category-1/article-3.html
3 category-2/article-7.html
4 category-3/article-21.html
when i go to
[subdomain.mydomain.com...] Redirects to->
http://www.example.com/?p=1
so when i change www.example.com to my actual address the browser stops the request because it is caught in an endless loop.
just to make sure we are on the same page here,
when [subdomain.mydomain.com...] is hit it should then goto
[subdomain.mydomain.com...] set in pids.txt)
Am i referencing the pids.txt file correctly? This is hosted on a windows box, w/ apache2
Thanks for your help.
Brian
For some reason, I always forget to clear the query string, and I did it again here.
Append a question mark to the substitution URL to clear the query string:
RewriteMap pages txt:c:/wamp/Apache2/conf/pids.txt
#
RewriteCond %{QUERY_STRING} ^p=([0-9]+)$
RewriteRule ^/$ http://www.example.com/${pages:%1¦NULL}[b]?[/b] [R=301,L]
Map files are sometimes a bit difficult to get working. Things like path differences and such are critical. I don't run Apache on Windows, so I can't help much with that part, but try putting that path into "windows format" with backslashes instead of forward slashes, and leaving out the drive name. You may then find the "just right" form that you need. I've never had a problem with that as long as the RewriteMap path matches or resembles the path specified for the server DocumentRoot.
Make sure the file permissions on your Map file permit read access from the Web context, and be sure that the Map file contents are written with a plain-text editor using ASCII characters and standard LF or CRLF line-enders.
Be aware that this new rule will only execute if your query string is exactly p=<numbers>. Your initial example had multiple parameters, so be sure to account for that difference.
Restart the server after any map or config file changes.
And again, beware of the broken pipe character problem mentioned in my first post.
Jim
it turns out it was the question mark. And I also was referencing the text file correctly.
just for anyone else who is looking for a similar solutions here is the working code:
RewriteMap pages txt:c:/wamp/Apache2/conf/pids.txt
RewriteCond %{QUERY_STRING} ^p=([0-9]+)$
RewriteRule ^/$ /${pages:%1¦NULL}? [R=301,L]
Thanks again!