Forum Moderators: phranque

Message Too Old, No Replies

301 redirect problem

Simple but frustrating problem

         

optic144

2:46 pm on Jul 15, 2006 (gmt 0)

10+ Year Member



I have this really simple but frustrating problem. Any help would greatly be appreciated. After successfully using mod_rewrite to make my URLs shorter, Google has still got my old URLs indexed.

I want to redirect:
[tdfn.example.com...]
To:
[tdfn.example.com...]
I've tried doing this:

RewriteEngine On
Options +FollowSymlinks
RewriteBase /
Redirect permanent /index.php?goto=Frontier/Kazemon/kazemon [tdfn.example.com...]

That doesn't work. I know the question mark is a quantifier in mod_rewrite, so I then tried doing this:

RewriteEngine On
Options +FollowSymlinks
RewriteBase /
Redirect permanent /index.php\?goto=Frontier/Kazemon/kazemon [tdfn.example.com...]
RewriteRule ^(.*)/$ index.php?goto=$1

And that doesn't work either. Please help me, this is really driving me crazy, such a simple problem yet so frustrating! Yet redirects work fine if the page it redirects from doesn't have a parameter. -sigh- Any help would greatly be appreciated.

[edited by: jdMorgan at 5:27 pm (utc) on July 16, 2006]
[edit reason]
[1][edit reason] No URLs, please. See TOS. [/edit]
[/edit][/1]

jdMorgan

5:32 pm on Jul 16, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The basic problem is that neither Redirect nor RewriteRule can 'see' a query string -- which is data attached to a URL to be passed to the resource at that URL. So, in your example, the URL is "index.php", and the attached query string is "goto=Frontier/Kazemon/kazemon"

In order to test and/or manipulate query strings, you'll need to use RewriteCond %{QUERY_STRING} followed by a RewriteRule that examines only the local URL-path of the request. For example:


RewriteCond %{QUERY_STRING} ^goto=Frontier/Kazemon/kazemon$
RewriteRule ^index\.php$ http://tdfn.example.com/Frontier/kazemon/ [R=301,L]

Jim

[edited by: jdMorgan at 5:32 pm (utc) on July 16, 2006]

optic144

3:10 pm on Jul 25, 2006 (gmt 0)

10+ Year Member



Thank you so much for your response... that works well ^^ However it leaves the query string in the browser bar.

This is the URL that shows up after the redirect:
[tdfn.example.com...]

It redirects to the right page but it looks... odd with the redundant query string. More importantly will this affect search engine ratings? Will the search engine index that long URL? Any help would greatly be appreciated again... it really would.

zwhalen

3:54 pm on Jul 25, 2006 (gmt 0)

10+ Year Member



I believe if you append a? to the end of the destination URL, it essentially resets the query string to null:

RewriteCond %{QUERY_STRING} ^goto=Frontier/Kazemon/kazemon$
RewriteRule ^index\.php$ http://tdfn.example.com/Frontier/kazemon/? [R=301,L]

I can't test it at the moment, but I'm pretty sure that's what someone here told me and I'm pretty sure that's still working on a site where I implemented that.

jdMorgan

11:01 pm on Jul 25, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That should work -- I always forget that part for some reason...

Jim

optic144

11:41 pm on Jul 25, 2006 (gmt 0)

10+ Year Member



Thanks for the help, I tried that but it doesn't seem to work. =( Could my Mod_Rewrite rule be conflicting with this?

My current .htaccess file:

RewriteEngine On

RewriteCond %{QUERY_STRING} ^goto=Frontier/Kazemon/kazemon$
RewriteRule ^index\.php$ [tdfn.example.com...] [R=301,L]

Options +FollowSymlinks
RewriteBase /
RewriteRule ^(.*)/$ index.php?goto=$1

RewriteCond %{HTTP_HOST} ^www\.tdfn.example\.com
RewriteRule ^(.*)$ [tdfn.example.com...] [R=301,L]

# Redirects

Redirect permanent /Tdfn [tdfn.example.com...]

RewriteRule ^Tdfn/Downloads/amvs.htm [tdfn.example.com...] [R=301,L]
RewriteRule ^Tdfn/Frontier/epguide.htm [tdfn.example.com...] [R=301,L]
RewriteRule ^Downloads/video.htm [tdfn.example.com...] [R=301,L]

# Custom 404 Page

ErrorDocument 404 /index.php?goto=error

jdMorgan

11:47 pm on Jul 25, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Did you flush your browser cache before testing?

Jim

optic144

8:37 am on Jul 26, 2006 (gmt 0)

10+ Year Member



Yes jdMorgan I have. Any more ideas? I'm sorry if this problem is bothering you...

optic144

2:58 am on Jul 29, 2006 (gmt 0)

10+ Year Member



As a temporary solution I used a PHP page with a redirect code in it, but Google doesn't seem to like it and removed some of my pages from its index. Anybody really don't have any more ideas? I really do wish this would work. Otherwise I will just have to wait until Google indexes the new ones.

If I may ask another question, if I can't redirect my old pages should I make them PHP pages with the HTTP header #403 - Gone?

jdMorgan

3:42 pm on Jul 29, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There is something fundamentally wrong with the code, but you don't say how "it doesn't work," and that phrase means almost nothing.

So, is it that the new rule almost worked, but stopped working when you added the trailing question mark? Or the whole .htaccess file doesn't work, or you get a server error, or what?

Tell us how you tested:

  • What local-URL-paths di you request?
  • What were the results of that test?
  • How did that result differ from what you expected?
  • Was there a server error during the test?
  • If so, what is in the server error log?
  • Did the server or browser report a redirection limit error?

    It's very difficult to remote-diagnose these problems, and you can increase the chances of getting a useful answer by being very thorough and specific.

    [added]
    Your code is disorganized, and this may lead you to misunderstand how it works. Here is your code re-arranged to show the order that the directives will execute on a typical server. Note that directives belonging to different Apache modules will execute in the order shown regardless of their order in your code.


    # Apache core directives - declare custom 404 Page
    ErrorDocument 404 /index.php?goto=error
    #
    # mod_aliasdirectives - redirects
    Redirect permanent /Tdfn http://tdfn.example.com/
    #
    # mod_rewrite directives - internal rewrites and external redirects
    Options +FollowSymlinks
    RewriteEngine on
    RewriteBase /
    #
    RewriteCond %{QUERY_STRING} ^goto=Frontier/Kazemon/kazemon$
    RewriteRule ^index\.php$ http://tdfn.example.com/Frontier/kazemon/? [R=301,L]
    #
    RewriteRule ^(.*)/$ index.php?goto=$1
    #
    RewriteCond %{HTTP_HOST} ^www\.tdfn.example\.com
    RewriteRule ^(.*)$ http://tdfn.example.com/$1 [R=301,L]
    #
    RewriteRule ^Tdfn/Downloads/amvs.htm http://tdfn.example.com/Downloads/amvs/ [R=301,L]
    RewriteRule ^Tdfn/Frontier/epguide.htm http://tdfn.example.com/epguide01/ [R=301,L]
    RewriteRule ^Downloads/video.htm http://tdfn.example.com/downloads/ [R=301,L]

    Note specifically that because mod_alias directives will be applied first on almost all servers, your two "Tdfn" rewriterules are unlikely to ever be applied -- the "Redirect permanent" directive will take precedence over these RewriteRules.
    [/added]

    Jim

  • optic144

    2:52 am on Jul 30, 2006 (gmt 0)

    10+ Year Member



    I feel extremely embarassed to say that it now works properly now. I sincerly apologise for not providing enough detail when I said it didn't work. To clarify, when I said it didn't work I meant that even by adding the preceding question mark at the end of the RewriteRule, the query string simply did not clear after it redirected. Strangely it does now and I can only imagine that the browser cache did not clear/flush properly due to my own carelessness.

    Thank you so much for your help jdMorgan, you have my most sincerest and deepest gratitude. ^^

    Yes I did have some confusion on how Apache executed it's directives within the .htaccess file. Thanks for the clarification, again very much appreciated.