Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to redirect URLs based on query string?

Tags:

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?

like image 947
user785179 Avatar asked Oct 25 '12 16:10

user785179


2 Answers

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]
like image 66
undone Avatar answered Sep 18 '22 21:09

undone


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>
like image 23
sebastian-greco Avatar answered Sep 19 '22 21:09

sebastian-greco