My first Drupal site is live now, and I'm in the process of adding some redirects to catch 404s coming from old bookmarks. I'm not getting the results I expect from my changes to .htaccess. I put in this:

Redirect permanent /file.html http://www.example.com/file

...but the visitor is redirected to http://www.example.com/file/?q=file.html

This works:

Redirect permanent /file.html http://www.example.com/?q=file

...but that conflicts with the Clean URL convention my visitors will expect. Is there a better method for accomplishing this?

Comments

robdinardo’s picture

I am about to launch a new version of a website and I wish to redirect some links. I am using IIS with ISAPI Rewrite 3, but I cannot seem to get the redirects to work. My .htaccess file looks like this:

RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

and it works as expected for clean URL's. But how do I redirect a URL to a different URL? I wish to do this without using the Global Redirect module. I tried adding...

RewriteRule ^oldurl$ /newurl [R=301] 

but it does not work. Can anyone help please?

robdinardo’s picture

Ok. order matters... and it makes sense! FYI for anyone out there pulling their hair out trying to understand .htaccess. The trick is to add the above line before the rewrite rule for the index.php page. Makes sense! So my .htaccess file now looks like this:

RewriteEngine on
RewriteBase /

# redirect some old URL's
RewriteRule ^oldurl$ newurl [NC,R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

Hope this helps anyone - I am sure I will have to look back at this post for reference. BTW, the 301 code means "moved permanently" and 302 means "Moved temporarily (default)"

TurtleX’s picture

Thanks! Just what I needed. Now my non-drupal redirects work again.

gr33nman’s picture

I've set this up exactly as suggested:

# Various rewrite rules.
<IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteBase /
  
  RewriteRule ^/beverage/refillables/biblio.html$     http://refillables.grrn.org/content/bibliography  [NC,R=301,L]

  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_URI} !=/favicon.ico
  RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
</IfModule>

The first domain is the main domain. The second domain is a subdomain of the first and also set up as multi-hosted under the same drupal core. I'm getting a hybrid answer: The correct return URL with a "?q=" and the old URL after it, causing 404's. I'd be grateful for some suggestions on what I've done wrong.

This:
http://www.grrn.org/beverage/refillables/biblio.html
yields this:
http://refillables.grrn.org/content/bibliography?q=beverage/refillables/biblio.html
when what I need is this:
http://refillables.grrn.org/content/bibliography

I've got hundreds of pages I need to redirect specifically.

I'm also getting the "?q=" behavior for anything going to other domains outside drupal core.
This: http://www.grrn.org/action/
Yields this: http://archive.grrn.org/action/?q=action/
Rather than this: http://archive.grrn.org/action/

ayesh’s picture

- nothing interesting in this comment -

gr33nman’s picture

Your original post suggested the module Path Redirect.
http://drupal.org/project/path_redirect

Thank you.

This suits my needs. I was able to import several hundred redirects to various new subdomains from their original urls in minutes. Thank you.

I'm curious why you removed your original suggestion. It seems to work fine.

- nothing interesting in this comment -

// Ayesh

ayesh’s picture

Oh, sorry. I thought you are looking for a way to redirect URLs with some wildcard patterns. AFAIK Path_redirect can't do this. I'm not sure you mentioned that in original posting so I didn't want to post junk content here :-)