homepage Welcome to WebmasterWorld Guest from 50.16.130.188
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Subscribe to WebmasterWorld

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
Dynamic 301 .htaccess Redirects not Working
What am I doing wrong?
adder




msg:4511716
 2:35 pm on Oct 24, 2012 (gmt 0)

Hi,

I'm trying to redirect old-style dynamic urls to a set of static urls using .htaccess
Wrote this code but it doesn't seem to be doing anything. Where's the mistake please?


RewriteEngine on
RewriteCond %{QUERY_STRING} ^pageid=325$
RewriteRule ^/site/index.cfm$ http://www.example.com/new-url.htm? [L,R=301]


Old url structure is:
http://www.example.com/site/index.cfm?pageid=325
and I want it to be:
http://www.example.com/new-url.htm

 

lucy24




msg:4511951
 9:06 pm on Oct 24, 2012 (gmt 0)

#1 When you say

/site/

do you mean your domain (example.com) or do you mean a subdirectory? A RewriteRule only sees the path: the part after http://www.example.com and before the query, if any. In htaccess, leave off the leading slash in the pattern. Include it in the target when you are rewriting alone. (Your Redirect target is correctly formed, so don't change it.)

#2 a rule that explicitly says
index.cfm$

will only pick up requests that either asked for this form in the first place (baaad!) or that have already been rewritten by mod_dir. This normally executes after mod_rewrite, but in htaccess you can't make assumptions so rules should allow for both forms:

RewriteRule ^directory(/(index\.cfm)?)?$ et cetera.

The double-nested ? elements are to avoid a double redirect if you get a request missing the final directory slash. Remember to escape all literal periods in the pattern. Sometimes it doesn't make much difference; sometimes it's crucial.

Are you really making 325+ separate rules, or is that just your example?

g1smd




msg:4512017
 11:49 pm on Oct 24, 2012 (gmt 0)

RewriteCond %{QUERY_STRING} ^pageid=325(&|$)
RewriteRule ^folder/(index\.cfm)?$ http://www.example.com/new-url.htm? [R=301,L]


The above corrects several faults.

Do you really need the new URL to end ".htm"? I would aim for extensionless URLs on the new site.

Additionally, if there are a large number of these redirects to perform, you would be better off not having a huge list of redirects in the htaccess file.

Instead you should internally rewrite (that's internally rewrite using a single rule, not externally redirect) all of these requests to a special PHP script that looks up the new URL in an array and uses the PHP HEADER directive to send a 301 redirect to the new URL.

RewriteCond %{QUERY_STRING} ^pageid=([0-9]+)(&|$)
RewriteRule ^folder/(index\.cfm)?$ /special-script.php?id=%1 [R=301,L]


You can use a language other than PHP if you want, this was just an example.

This internal rewrite must unusually appear near the beginning of the htaccess file (after the rules that block access, and before rules that redirect). You MUST also ensure that these requests cannot be redirected by the non-www/www rule in the htaccess file (add a RewriteCond exclusion to that rule) otherwise non-www requests for the old URLs will expose the special-script internal path back out on to the web as a new URL and create a double redirect chain for those requests.

That would be a disaster.

g1smd




msg:4512146
 7:35 am on Oct 25, 2012 (gmt 0)

The "special script" should also return a 404 Not Found header and "include" your 404 error page code and content when the requested ID number doesn't exist.

adder




msg:4512271
 2:27 pm on Oct 25, 2012 (gmt 0)

@lucy24, yes, I actually meant a subdirectory
It's not 325 redirects, I'll only need 18 of them :)

@g1smd, genius. It's working now! Thank you.

I cannot say I understood the last paragraph but it does sound like a disaster and I'll have to read something about .htaccess. However, after uploading the .htaccess I tried both
http://www.example.com/site/index.cfm?pageid=325
and
http://example.com/site/index.cfm?pageid=325
They redirect to http://www.example.com/new-url.htm which I suspect is a good thing :)

g1smd




msg:4512313
 4:16 pm on Oct 25, 2012 (gmt 0)

Yes. You must test with both www and non-www requests. Use the Live HTTP Headers extension for Firefox to look in detail at the reponses. You need to make sure there are no chains. Try also adding another junk parameter on the end of the request and make sure that request is also correctly redirected.

With only 18 redirects, doing this in htaccess is fine.

You may want to try out the "rewrite to a script" version so that you are familiar with it for when you need it on a really big site. I'm using it to redirect 80 000 duplicate content and old-style URLs to 1200 new extensionless URLs on one site.

The stuff about correct "404" responses was for when you use a PHP (or other) script not a pure htaccess solution.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved