Forum Moderators: coopster

Message Too Old, No Replies

Redirect 301 if not the correct permalink

         

cheaperholidays

8:25 am on Oct 5, 2011 (gmt 0)

10+ Year Member



Hi all

I wonder if you can help?

We have a travel advice blog and we are getting odd urls showing up as 404 errors.

This is such an error - [yada...] yada.com/travel/foreign-travel-advice-portugal/2011/06/01/.Portugal

The correct url is [yada...] yada.com/travel/foreign-travel-advice-portugal/2011/06/01/

How can i make sure that only the correct url is shown, i have tried url redirect plugins but they only keep the structure correct not remove additions to the url

They would take this url [www....] yada yada.com/travel/foreign-travel-advice-portugal/ and replace it with:

[yada...] yada.com/travel/foreign-travel-advice-portugal/2011/06/01/


Kind regards

Mike

g1smd

8:57 am on Oct 5, 2011 (gmt 0)

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



Use example.com in this forum.

cheaperholidays

9:02 am on Oct 5, 2011 (gmt 0)

10+ Year Member



Ok but that is not my url just an example....

g1smd

9:09 am on Oct 5, 2011 (gmt 0)

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



Use example.com is this forum to stop forum URL auto-linking.

cheaperholidays

9:25 am on Oct 5, 2011 (gmt 0)

10+ Year Member



Yes yes but do you have any ideas how i can code a 301 redirect ?

g1smd

10:20 am on Oct 5, 2011 (gmt 0)

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



Of course we do, there's 50 000 previous threads with examples.

But we want read the question. Clearly.

cheaperholidays

10:33 am on Oct 5, 2011 (gmt 0)

10+ Year Member



Ok how do i use php to prevent a blog post from having extra characters added to it, then redirect beck to the original post as my original query.

this travel/foreign-travel-advice-portugal/2011/06/01/.Portugal needs to redirect back to this
travel/foreign-travel-advice-portugal/2011/06/01/

cheaperholidays

10:48 am on Oct 5, 2011 (gmt 0)

10+ Year Member



g1smd Will you kindly talk sense, php may be easy to you but i have no idea.

So can you point me in the right direction

enigma1

4:00 pm on Oct 5, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You would need to have some code in place and find the right links if the request returns back 404. Or if the url module you use hooks all links it can find the right one.

One way is by using some basic search having the links stored in a database.

right link
/travel/foreign-travel-advice-portugal/2011/06/01/

request
/travel/foreign-travel-advice-portugal/2011/06/01/.Portugal

Get the request check in the database for links like
/travel/foreign-travel-advice-portugal/2011/06/01/.Portugal
If nothing found start removing characters or words and check again. If nothing found redirect to the home page.

Another method can be implemented if the urls module constructs the links based some separator with a specific order of arguments.

So if say the first argument is always a category
/travel
And the 2nd argument is a text page
/foreign-travel-advice-portugal
And the 3rd,4th,5th arguments represent a date
You could split the requested url using the separator and search the database for the associated entities. Therefore knowing the string format and location of the date you could scan the database only for articles posted in the specific date and each time trim only one of the input fields.

This second method may find the right url faster but if one of the critical characters like the separator or the order of arguments is incorrect it will be harder to locate it. The first method is more expensive in terms of resources and may take longer as it will scan for all links stored, but can be used for any web engine.

cheaperholidays

10:33 am on Oct 6, 2011 (gmt 0)

10+ Year Member



Hi Thanks for that, but this is not being loaded from our database but from google picking up incorrect links from other sites.

Does that make sense? I was thinking about some htaccess redirect maybe

Mike

enigma1

11:59 am on Oct 7, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If search engines pickup incorrect links and report only in the wmt then you shouldn't worry about it. They will drop it after a bit.

But if they keep them in their index, it means somehow these links were found inside your site's pages. And many web apps I've seen have this problem. You will have to fix the code in the application. Redirects via .htaccess won't be effective because is likely always be new links spiders pick up.

Rule of thumb you only generate links with parameters the application is aware of. And because of the seo links these applications generate they must have a proper decoder to validate each parameter they're aware of. If it's invalid they should redirect or give 404 or simply present the closest page without propagating the invalid parameters.

So in what you posted, if the seo decoder knows that no parameters should follow the date field say, it will ignore /.portugal and not generating links with that appended, or issue a redirect etc.

It's somewhat tricky to test, but try pages that have pagination links, comments etc, append some invalid parameters or rearrange the parameters eg:
/travel/foreign-travel-advice-portugal/2011/06/01/invalid1/invalid2/invalid3
/travel/2011/06/01/invalid1/invalid2/invalid3/foreign-travel-advice-portugal/
then check if these invalid words/phrases are shown somewhere in the html source. If they do, you have a problem in the code.

You see a cheap way of creating seo link decoders, is to use separators and/or positioning of parameters to pull a page without cross-referencing the database against each parameter or against the url itself. And that has lots of side effects.

cheaperholidays

12:35 pm on Oct 7, 2011 (gmt 0)

10+ Year Member



Thanks Enigma 1 for that very informative post, the links are coming from external links and are generating 404's , so for now i am issuing a 301 redirect for every one i find bit laborious.

But i notice you say leave it and google will drop the 404's the redirect plugin will not issue a redirect with this link /travel/foreign-travel-advice-portugal/2011/06/01/.Portugal it ignores it.

I emailed the plugin coder, and he suggested, If you just want to remove .Portugal/ at the end of URL, you might wish to write a mod_rewrite directive for Apache, which would be easier. But would this redirect all additions to the url not just .portugal

rocknbil

3:18 pm on Oct 7, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try this. The regex may be incorrect but

RewriteRule (^[a-z\-\d/]+)\..+$ /$1 [R=301,L,NC]

The idea is "anything containing letters, numbers, digits, dash, or / and ending in a dot followed by any character." You may need to do a conditional request prior to the query string, like

RewriteCond %{REQUEST_URI} !css\/
RewriteCond %{REQUEST_URI} !images\/
RewriteCond %{REQUEST_URI} !js\/
RewriteRule (^[a-z\-\d/]+)\..+$ /$1 [R=301,L,NC]

Note this will also redirect anything with a dot, like .html, .gif, .jpg, etc, which is why you might need to do some conditionals to disallow the directories for files in which you'd have an extension.

as I said it may not be exactly right so you'll have to play with it.

g1smd

12:12 am on Oct 27, 2011 (gmt 0)

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



Target URL in the RewriteRule should also specify the protocol and domain.

Slashes should not be escaped.

!css\/
!images\/
!js\/

simplifies to
!(css|images|js)/

cheaperholidays

3:43 pm on Nov 2, 2011 (gmt 0)

10+ Year Member



Thanks everyone for your help...


Much appreciated..