Forum Moderators: phranque

Message Too Old, No Replies

301 Redirects

         

jinxed

9:25 pm on Dec 7, 2010 (gmt 0)

10+ Year Member



I have a really straightforward .htaccess issue that should be very simple – but I cannot seem to implement.

I have around 20 URLS in the following format;

http://www.example.com/folder1/folder2/drink/2/

I am currently dynamically rewriting the 20 pages to these URLs from the htaccess file located in folder2. This isn’t a problem.

For a new design, I want each of the URLs to be http://www.example.com/folder1/folder2/keyword.html

I therefore decided that as there is no obvious rule I could implement here that it would be best to add 20 individual 301 redirect lines i.e.

Redirect 301 /drink/2/ http://www.example.com/folder1/folder2/keyword.html

This is returning a 404 Not Found error.

Can someone advise why this error occurs? Am I missing something obvious here?

jdMorgan

3:52 am on Dec 8, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, likely the distinction between mod_alias and mod_rewrite as regards URL-path localization and matching, or perhaps the point that the "Redirect" directive uses prefix-matching, and that any URL-path-part appended to the client request but not specified in the Redirect match string will be appended to the substitution URL-path. e.g. your Redirect redirects requests for www.example.com/drink/2/anything-here to www.example.com/folder1/folder2/keyword.htmlanything-here".

See Apache mod_alias "Redirect" directive documentation for details.

One thing to consider is to continue to rewrite these old-URL requests to your script, where you may be able to use your existing database to easily "look up" the correct redirect URL, based on the association of "drink/2" with "keyword" in your database.

Another thing to consider is that adding ".html" to your new URLs gives no benefit, and only makes the URL longer. Many threads here [webmasterworld.com] in the past few years have discussed going *to* extensionless URLs, rather than adding extensions to URLs. On my sites, I'd use "example.com/folder1/folder2/keyword" as the URL -- unless I could identify a good way to get rid of "/folder1/folder2" as well... :) This of course depends on your site, your goals, and whether "folder1" and "folder2" are actually additional keywords or not.

Jim

jinxed

8:36 pm on Dec 8, 2010 (gmt 0)

10+ Year Member



Thank you for your help Jim,

I have decided that the best approach for me here would probably be your second suggestion.

I have implemented this with the php 301 header redirect, which seems to functioning fine.

Code:

header ('HTTP/1.1 301 Moved Permanently');
header ('Location: '.$location);

As for your extension URL suggestion, I also conclude this is the way forward. I had planned to use this format for my site redesign.

jdMorgan

11:22 pm on Dec 8, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



"Seems to be functioning fine."

I'd advise that you check your server response using the "Live HTTP Headers" add-on for Firefox and Mozilla-based browsers (or a similar add-on) to be sure... Verify that the status code returned by the server is indeed "301".

Jim

jinxed

7:09 pm on Dec 9, 2010 (gmt 0)

10+ Year Member



"Live HTTP Headers" did indeed confirm that the 301 status code was being produced.

Again, thanks Jim, your help here is invaluable.