Welcome to WebmasterWorld Guest from 54.160.163.163

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

301 Redirect result to 404 Error

     

zykes

5:36 am on Jun 27, 2013 (gmt 0)



Hello,

I have a problem with a dynamic rewrite and 301 redirect rule. I'm testing a 301 redirect on a localhost server, details below is my htaccess file content

RewriteCond %{QUERY_STRING} ^id=ABC-A$
RewriteRule ^product\.php$ http://localhost/mywebsite/products/Master-Product-Key? [R=301,L]

The normal dynamic url of the product in localhost setting is,
http://localhost/mywebsite/product.php?id=ABC-A and when I type this url it redirects to the changed URL http://localhost/mywebsite/products/Master-Product-Key but the problem is, I got a 404 Not Found error.

Can anyone help?

[edited by: phranque at 11:54 am (utc) on Jun 27, 2013]
[edit reason] unlinked urls [/edit]

lucy24

6:46 am on Jun 27, 2013 (gmt 0)

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



... and that's why they keep telling you to use example dot com in posts. Not "mydomain" or "somesite" or anything else. Only "example" will do.

I find it simpler to leave off the protocol-plus-domain when I'm testing locally (MAMP in my case). Just use a leading slash. Nobody but you is in there, so you don't have to worry about path injections or malformed domain names or any of the other stuff you normally have to guard against.

Edit after looking at de-obfuscated URLs:
Your redirect seems to be doing exactly what you've told it to do. Request A, redirect to B. That means the problem is somewhere else.

Crystal ball says you have half of the redirect-to-rewrite two-step but you are missing the other half.

An URL ending in "Master-Product-Key" with no extension is obviously not a physical file. So further along in the htaccess you need a rule that rewrites the request to serve content from wherever it really lives. If you don't have this part, the server will look for "Master-Product-Key" and will come up empty-handed. That's your 404.

zykes

7:17 am on Jun 27, 2013 (gmt 0)



sorry lucy... still the 404 error is displayed.

but do you think this will work if put on the live site?

RewriteEngine on
RewriteCond %{QUERY_STRING} ^id=ABC-A$
RewriteRule ^product\.php$ http://www.example.com/products/Master-Product-Key/? [R=301,L]

I just want to make some additional inputs from you guys before putting it live.

[edited by: phranque at 11:54 am (utc) on Jun 27, 2013]
[edit reason] Please Use Example.com [webmasterworld.com] [/edit]

lucy24

7:50 am on Jun 27, 2013 (gmt 0)

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



You haven't explained what's behind "Master-Product-Key". Or same with directory / at the end. If this second form is correct, are we talking about a real, physical directory with an index page?

On the whole, on balance, what with one thing and another, I would generally tend to think it's not a 100% good idea to go live with a redirect that leads to a 404.

zykes

8:05 am on Jun 27, 2013 (gmt 0)



yeah, I wouldn't let this go live at this scenario.

"Master-Product-Key" is not a physical directory with an index page. the dynamic page of the URL with "Master-Product-Key" should be http://www.example.com/product.php?id=ABC-A, so what I wanted was when I access http://www.example.com/product.php?id=ABC-A, the URL that I should be seeing is http://www.example.com/products/Master-Product-Key/ instead of http://www.example.com/product.php?id=ABC-A
that's what I want to achieve actually with the rewrite-redirect rule that I have.
Sorry, I'm a bit noob with htaccess rules.

[edited by: phranque at 11:55 am (utc) on Jun 27, 2013]
[edit reason] Please Use Example.com [webmasterworld.com] [/edit]

lucy24

9:37 am on Jun 27, 2013 (gmt 0)

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



You've got the redirect down solid. But where's the rewrite?

Once the browser has made its fresh request for "Master-Product-Key" you have to tell the server to go back to long-boring-url.php?more-stuff-here to find the content to make the page.

But you can't write the rule until you've got a firm grip on what's really happening.

Crystal ball, now working overtime, strongly suggests you've misunderstood what a rewrite is. You would not be the first person to get it backward :) (It is not possible to have the "rewrite" concept a little bit wrong. It's either exactly right or 180 degrees off.)

g1smd

4:41 pm on Jun 27, 2013 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



You need two RewriteRules.

One externally redirects requests for "horrible" URLs to "friendly" URLs.

The other internally rewrites requests for "friendly" URLs to fetch the content from the "horrible" internal location.

You have missed out this second rule.
 

Featured Threads

Hot Threads This Week

Hot Threads This Month