301 Redirect going to Long URL - why?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

301 Redirect going to Long URL - why?

Post by simonmlewis »

Code: Select all

DirectoryIndex index.php index.html index.htm 
order allow,deny
allow from all 

ErrorDocument 404 /home

Options +FollowSymLinks
Options +Indexes
RewriteEngine On

Redirect 301 /blog/comments/feed/ http://www.site.ie/error
Hi.
We are adding a load of 301s, but rather than going to /error, they are going to /error?page=.......
What it is also doing, is if the bad URL is a shorturl, the URL shown after error?.... is the LONG url version?!?!?!?!

Am I doing the 301 correctly, should it be placed after all the other mod rewrites?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: 301 Redirect going to Long URL - why?

Post by requinix »

You are doing it somewhat correctly but it might not be what you want it to do. Redirect works by taking everything after the .../feed/ and appending it to the /error, so if there's a query string then you'll get it appended on the error URL too.

Redirects suck. How about doing the "redirect" internally? The user sees the same URL but they get the error page. Use mod_rewrite instead of a Redirect.

Code: Select all

RewriteEngine on
RewriteRule ^/?blog/comments/feed/ <whatever page actually handles "/error"> [L]
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: 301 Redirect going to Long URL - why?

Post by simonmlewis »

No that isn't a "301", that's just telling it to take one page to another.
We need to tell Google that the page is wrong, and it's permanently going to /error instead.

Why would our code be rewritting the URL in "long" form aswell??
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: 301 Redirect going to Long URL - why?

Post by requinix »

simonmlewis wrote:No that isn't a "301", that's just telling it to take one page to another.
We need to tell Google that the page is wrong, and it's permanently going to /error instead.
That's right: you do need to tell Google the page is wrong. But the way you do that is not with a 301 but with a 404. Which would be served by that error page - which I assumed it was already doing.

A redirect of any sorts is the wrong message to send. Leave the URL the same and serve an error page with a 404 instead. Do that either by having the site code realize the requested page does not exist, or in this case by rewriting to the error page.
simonmlewis wrote:Why would our code be rewritting the URL in "long" form aswell??
"Long form"? You mean a filename? Because that's what mod_rewrite wants to get: a filename. Which is because Apache already had a filename by that point and doesn't want to have to look up yet another URL. Not to say a subrequest (URL lookup) is impossible, it's just less desirable.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: 301 Redirect going to Long URL - why?

Post by simonmlewis »

We have been told by our guys that we need to do a 301, that tells Google what to do.
Therefore this is what we need it to do - a Redirect 301 from a > b.

How would we do multiple 404s in HTACCESS to cover over a thousand pages?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: 301 Redirect going to Long URL - why?

Post by simonmlewis »

Code: Select all

DirectoryIndex index.php index.html index.htm 
order allow,deny
allow from all 

ErrorDocument 404 /home

Options +FollowSymLinks
Options +Indexes
RewriteEngine On

Redirect 301 /simon http://www.site.ie/home
Here is a great example. This takes me to:
[text]http://www.site.ie/home?page=simon&menu=home[/text]
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: 301 Redirect going to Long URL - why?

Post by requinix »

Huh. At 2am I don't seem to care about the quality of the SEO advice you're receiving.

Like I said earlier, Redirect will add the query string. IIRC you can tell mod_rewrite not to do that, though I'm not sure whether it'll redirect you to literally "/error?".

Code: Select all

RewriteEngine on
RewriteRule ^/?blog/comments/feed /error? [L,R=301]
Now, what does that example have to do with anything? Have you changed your mind about how this page we've been looking at should redirect?
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: 301 Redirect going to Long URL - why?

Post by simonmlewis »

301 just are not working for us,. but standard redirections are really doing the trick. But here's one that will not work for us:

Code: Select all

DirectoryIndex index.php index.html
order allow,deny
allow from all 

Options +FollowSymLinks
Options +Indexes
RewriteEngine On

RewriteRule ^categ/page///1/?$ index.php?page=selectpage&menu=home [L]
This just goes to a full on
[text]Not Found
The requested document was not found on this server. [/text]
...page.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: 301 Redirect going to Long URL - why?

Post by Celauran »

simonmlewis wrote:

Code: Select all

RewriteRule ^categ/page///1/?$ index.php?page=selectpage&menu=home [L]
What is that supposed to match?
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: 301 Redirect going to Long URL - why?

Post by simonmlewis »

It's meant to go to index.php?page=selectpage&menu=home.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: 301 Redirect going to Long URL - why?

Post by Celauran »

I meant the regex at the beginning. ^categ/page///1/?$

http://yoursite.co.uk/categ/page///1 seems a pretty unlikely URL.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: 301 Redirect going to Long URL - why?

Post by simonmlewis »

Youv'e lost me.....
the /// is completely wrong, BUT Google has cached it at some stage, so need to tell it to go elsewhere.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: 301 Redirect going to Long URL - why?

Post by simonmlewis »

The other part of this issue now is an entry I am trying like this:

Code: Select all

RewriteRule ^ip_mobilehome.php?url=http://www.site.co.uk/product/512/HOLD/312/HOLD/372/R9-P90S/?$  index.php?page=selectpage&menu=home [L]
It won't go to the 'selectpage' URL, it goes to ip_mobilehome.php.

Why??

It works for most other URLs we are trying to 301.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
jdhmtl
Forum Newbie
Posts: 18
Joined: Tue Aug 12, 2014 7:33 am

Re: 301 Redirect going to Long URL - why?

Post by jdhmtl »

Something like this, perhaps?

Code: Select all

RewriteCond %{REQUEST_URI} ip_mobilehome.php
RewriteCond %{QUERY_STRING} ^url=http://www.site.co.uk/product/512/HOLD/312/HOLD/372/R9-P90S/?$
RewriteRule (.*)  index.php?page=selectpage&menu=home [L]
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: 301 Redirect going to Long URL - why?

Post by simonmlewis »

Would you mind explaining each line of that?
We have over a 1000 lines like this that work, but only this one (tho there are about 600 of them) that have this problem.
It reads like I'd need line one of our code just once, and then line two for each error one.
And line three just once.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Post Reply