help required using .htaccess to do a 301 redirect

Events happening in the community are now at Drupal community events on www.drupal.org.
wavesailor's picture

Hi,

I have a page with a URL alias that had a spelling mistake. I want to fix it but I also don't want to lose the traffic that has been coming to the site via the misspelt alias. So I added the following line to my .htaccess file just below Drupal's rewrite rules:
  redirect 301 /Hampton-Court-SA http://www.mysite.co.za/hampton_park_sa

This would have worked except for the fact that Drupal seemingly adds a query string at the end so the redirect then fails. The URL in the browser ends up being this:
  http://www.mysite.co.za/hampton_park_sa?q=Hampton-Court-SA

Any ideas or suggestions??

Now this is only the first part of my problem. The second part is a little more difficult in that I'm using one Drupal code-base (Multi-Site) so when I add the redirect line above, it will work for any domains that use this code-base. Okay it's unlikely to have exactly the same last part of the URL but I would prefer to do it correctly.
When I use my CPanel at my ISP to generate the redirect it creates something like this which does not work either:

  #Created by Cpanel but does not work
  RewriteCond %{HTTP_HOST} ^mysite.co.za$ [OR]
  RewriteCond %{HTTP_HOST} ^www.mysite.co.za$
  RewriteRule ^Hampton-Court-SA$ "http:\/\/www.mysite.co.za\/hampton_park_sa" [R=301,L]

I've googled around and tried different combinations but to no avail :-(

Any ideas or suggestions??

Comments

Path redirect

Max_Headroom's picture

Have you tried path redirect:
http://drupal.org/project/path_redirect

Quentin

.htaccess seems so simple to use ...

wavesailor's picture

Thanks - I did take a look at Path Redirect and Global Redirect and I'm sure they would work but it seemed a bit overkill for something so simple which is why I favoured .htaccess - yet as they say nothing is ever simple :-)

Be careful with .htaccess

perceptum's picture

Just be careful using .htaccess. Drupal uses other files as security measures to protect directories etc.

Also - if you upgrade core you could inadvertently overwrite your custom .htaccess stuff.

My vote is the modules. Max_Headroom++

:)

--
Bryan Gruneberg
Chief ninja in charge at Perceptum Thought Squad

Google Talk bryan@perceptum.za.net
Skype perceptum-ts

deep, dark .htaccess magic

burningdog's picture

The answer to your question is simple when you know how, and very hard when you don't - .htaccess is a dark place, full of voodoo and magic. Summoning forth from the depths of, uh, somewhere:

RewriteRule ^Hampton-Court-SA http://www.mysite.co.za/hampton_park_sa [R=301,L]

This ensures that no querystring stuff gets appended to the new url.

You'll have to experiment with the domain part of that, or try adding this before the above line:

RewriteCond %{HTTP_HOST} ^www.mysite.co.za$ [NC]

Let us know how it turns out.

deep, dark .htaccess continues....

syka's picture

It's a pity that @wavesailor did not tell us how and if he solved the problem. I would be really interested.
Anyway.. I have exactly the same problem. Redirect works but gives the extra argument ?q=something and by RewriteRule I don't know how to do it. I have tried sooooo many configurations, including the one by @Roger Saner and nothing. The default Drupla RewriteRule and the Clean URL are messing with everything. Very annoying.

Does anyone solved the problem? The path_redirect doesn't allow to redirect aliases I mean...
I have site www.my.site/node/333 with the alias www.my.site/alias333
I want to redirect www.my.site/alias333 to www.external.site/alias333

Hope someone can help me.
Thanks.

Solved

syka's picture

LooL... it took me like 10 min after I post the last message. The problem was not with the directive, but with its ORDER in the .htaccess file. :-) yeah...

so the directive:
RewriteRule ^alias333(.*)$ www.external.site/alias333/ [R=301,L]

must be before this one:
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

the magic, deep, dark .htaccess ...

Hi Syka, Yes that was my

wavesailor's picture

Hi Syka,

Yes that was my solution too - The order in which you put things in the .htaccess file

BUT I must agree with Roger that the module "Path redirect" is the correct and most efficient way to go. Everytime you update Drupal core your .htaccess get over-written and then you have to remember to put your changes back in ... a real pain.

Glad you found a solution,

burningdog's picture

Glad you found a solution, @syka! I'd probably still lean towards using the path_redirect module, because otherwise every time you upgrade your site, you have to remember to make the change to .htaccess again - where path_redirect stores the alias in the database.

Redirect with wildcard

Max_Headroom's picture

Hi
I can't use path redirect as my redirect contains a wildcard.

I need to redirect user to a custom profile page when clicking on /user/{uid}

In .htaccess this works fine:
RewriteRule ^user/(.*)$ /member/$1/wall [R=301,L]
Which takes me to /member/{uid}/wall

But I got a problem now with /user/{uid}/edit, it redirects me to /member/{uid}/edit/wall.

How can I make it still go to /user/{uid}/edit (or even to another url)?

Thanks

Quentin

Maybe you can rather use menu

stewest's picture

Maybe you can rather use menu token module?

That way you can use a token (alias) for the UID.

Hey Quentin, Have you tried

guybedford's picture

Hey Quentin,

Have you tried something like -

RewriteRule ^user/(\d*)$ /member/$1/wall [R301,L]

RewriteRule ^user/(\d*)/edit$ /member/edit/$1 [R301,L]

the \d should just match the numeric user id, and not include slashes etc.

Not sure how you then match the users/[username] url though, as there's no id in that though.

Remember to clear your browser cache

rooby's picture

I had the problem that I was being redirected to the new site but with the extra query string being added.

I changed the redirect RewriteRule to what should have worked but it still didn't.
In the end it was because firefox had cached the old broken redirect and kept serving me that instead of reading the new redirect on the server.

So I clear my browser cache (ctrl+shift+del in firefox) and it all works as expected.

Just add another alias

christiaan_'s picture

Regarding the original question of the thread "I want to fix it but I also don't want to lose the traffic that has been coming to the site via the misspelt alias".

A very easy workaround if you are using clean-url's is to just add another url alias for the node, leaving the old alias in place. This way both will point to the same node and no traffic will be lost, and no .htaccess voodoo needed.

Comment spam

asb's picture

Hi,

I have a similar problem; for a couple of weeks, one of my sites is being hit by comment spam bots; I'm running Mollom, so they seem to be scanning the paths /comment/reply/* and /reply/* systematically. Whatever they are doing, they issue dozends of requests per seconds, which significantly slows Apache2 down. During these "attacks", the load on my dedicated quad-core server peaks around 120 (being 4 = full load).

At least temporarily, I disabled the comment module and configured search404 not to do any full text searches anymore; as far as I understand, Apache is now delivering with it's raw performance (no database hits anymore, just 404). I'm now wrapping my head around those invalidated URL paths. Since accesses to those paths can only derive from bots, I'd like to send them somewhere else (e.g. to 127.0.0.1). However, so far those attemps had the opposite effect (increasing server load). Am I on the roghttrack with wildcard URL rewriting in .htaccess, or is there something else better suited?

Thanks!

asb,Use the

Michael-IDA's picture

asb,

Use the http://drupal.org/project/httpbl module in addition to Mollom. If your site is D6, use w/ this patch http://drupal.org/node/1855304 to eliminate comment spammers.

Best,
Sam

Edit: The order of .htaccess is important in Drupal. See: https://drupal.org/node/38960

South Africa

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds:

Hot content this week