21

I successfully mass migrated a Wordpress site to Drupal. Unfortunately in Wordpress, the content URL's were something like www.example.org/?p=123. My domain is still the same, but I want to do a redirect via htaccess as Drupal will not allow URL's to be www.example.org/?p=123. In other words, the content does not have the same URL as it did in Wordpress. For example, the new Drupal URL would be something like www.example.org/content/MyNewPage

I tried this in my .htaccess file and it does not work

Redirect 301 /\?p=375 http://www.example.org/content/MyNewPage

So I tried the below, but it does not work either.

Redirect 301 /\?p\=375 http://www.example.org/content/MyNewPage

Just as a test, I tried the below and it worked.

Redirect 301 http://www.example.org http://www.google.com

I made sure that my Redirect rule is at the top of the list in my .htaccess so it will be evaluated first. How do I fix this?

2 Answers 2

40

neither Redirect nor RedirectMatch allow you to specify a query string for the redirect source. [Source]

You have to use mod-rewrite for redirecting based on query string:

RewriteCond %{QUERY_STRING}  ^p=375$
RewriteRule (.*)  http://www.example.org/content/MyNewPage?  [R=301,L]
3
  • Unfortunately I get redirected to a 404 page not found, and my URL is still stuck showing example.org/?p=375. I can confirm that the "content/MyNewPage" does exist and works. I also can confirm that my RewriteRules are working because they work fine in Drupal.
    – user785179
    Oct 25, 2012 at 17:12
  • Clear your browser's cache and try again, browsers cache 301 Permanent redirects
    – undone
    Oct 25, 2012 at 17:16
  • Good suggestion, but now Firefox says that it is redirecting as an endless loop. "Firefox has detected that the server is redirecting the request for this address in a way that will never complete." The URL is coming out as example.org/content/MyNewPage?p=375
    – user785179
    Oct 25, 2012 at 17:20
1

You may consider use ModRewrite in your htaccess

<IfModule mod_rewrite.c>

RewriteEngine On

RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond %{QUERY_STRING}     ^p=345$    [NC]
RewriteRule index.php content/MyNewPage [NC,L,R=301]

</IfModule>

And you also may want to pass the old page id to the new URL concatenated (or maybe by QS?):

<IfModule mod_rewrite.c>

RewriteEngine On

RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond %{QUERY_STRING}     ^p=(.*)$    [NC]
RewriteRule index.php content/MyNewPage-%1 [NC,L,R=301]

</IfModule>
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.