Forum Moderators: phranque

Message Too Old, No Replies

Redirection not working properly. pls help

         

born2run

4:03 am on Dec 30, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Hi I have example.net redirecting to www.example.com with this in .htaccess:

RewriteCond %{HTTP_HOST} ^example.net$ [OR]
RewriteCond %{HTTP_HOST} ^www.example.net$
rewriterule ^(.*)$ http://www.example.com [R=301,L]

However when I go to example.net/any-valid-page-url it takes me to www.example.com and not example.com/any-valid-page-url

How do I fix the .htaccess to fix this url mess? Thanks!

not2easy

5:07 am on Dec 30, 2015 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



The "$" at the end of the condition is missing from the target URL in your rule. Add that right after .com to go to the captured URL. There are more efficient ways to write the rule, but that is the reason it doesn't request the entire URL.

deuces

5:08 am on Dec 30, 2015 (gmt 0)

10+ Year Member Top Contributors Of The Month



Try this code on your old site's .htaccess file:

RewriteEngine on
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

lucy24

7:04 am on Dec 30, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



RewriteCond %{HTTP_HOST} ^example.net$ [OR]
RewriteCond %{HTTP_HOST} ^www.example.net$
rewriterule ^(.*)$ http://www.example.com [R=301,L]

However when I go to example.net/any-valid-page-url it takes me to www.example.com and not example.com/any-valid-page-url
Yup, that's what you're telling it to do. You've captured the request, but you forgot to put the capture into the target as
www.example.com/$1

Incidentally, there's no need for the two separate Conditions. Just
RewriteCond %{HTTP_HOST} example\.net
without anchors. You should escape the period as \. but in this situation it's a non-lethal error. Is this in the same htaccess file that contains your example.com files? If no other hostname uses this htaccess, you don't need a condition at all.

born2run

8:19 am on Dec 30, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Ok I tried the following:

====
RewriteCond %{HTTP_HOST} ^example.net$ [OR]
RewriteCond %{HTTP_HOST} ^www.example.net$
rewriterule ^(.*)$ http://www.example.com/$1 [R=301,L]
====

As advised I put the $1 in front. But it's not working... the redirection is to the main url www.example.com not the article url. Kindly assist. Thanks!

lucy24

7:35 pm on Dec 30, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Before anything else, can you please change it to
RewriteRule
in CamelCase? Casing in Apache is an iffy subject,* but it gives me the fantods so let's stick with the standard formats.

Now then: How many different hosts pass through this specific htaccess file? In particular, do example.net and example.com use the same htaccess?


* I am sorry to say that I had to go over to That Other Forum to find the relevant link [httpd.apache.org], since I can never find anything with apache dot org's own search functions.

born2run

4:16 am on Dec 31, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Yes example.net and example.com use the same htaccess...kindly assist. Thanks

born2run

4:18 am on Dec 31, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Changing the case didn't help either. What is it that I"m doing wrong?

born2run

4:39 am on Dec 31, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Hi I think I've figured this one out.. the code above works when I emptied my firefox cache.. so thanks so much for your help guys! :)

lucy24

4:41 am on Dec 31, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Changing the case was just to calm me down; Apache functions aren't really case-sensitive ;)

Did you also try reducing the Condition to the simpler form
RewriteCond %{HTTP_HOST} example\.net
without anchors?

What's the current problem: redirect not happening at all, or redirect sending everything to the root? If you comment-out the whole package, do you end up at exactly the URL you requested?

Have you tried this in multiple browsers and/or empty the cache and/or reload the page? Browsers will cache redirect responses; this may be the single most embarrassing of overlooked explanations. (I can't begin to explain why it has never happened to me. Everything else does.)

Edit:
Tralala! Obviously we were typing at the same time.

born2run

6:59 am on Dec 31, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Hi yes Lucy thanks a lot!